位元組 Go 郎求職攻略:履歷表準備:突顯 Go 經驗與技能,量化專案成果。筆試複習:刷演算法題,掌握 Go 基礎和並發特性。面試準備:深入理解 Go,了解位元組技術棧,準備專案經歷和演算法問題。實戰案例:建構 RESTful API,體現解決問題能力。
Go 郎進位元組求職攻略大全
目錄
##履歷表準備
筆試複習
面試準備
實戰案例
建立一個簡單的Go 語言RESTful API
package main import ( "encoding/json" "fmt" "net/http" "github.com/gorilla/mux" ) type Person struct { ID int `json:"id"` Name string `json:"name"` } var people []Person func main() { r := mux.NewRouter() r.HandleFunc("/people", getPeople).Methods("GET") r.HandleFunc("/people/{id}", getPerson).Methods("GET") r.HandleFunc("/people", createPerson).Methods("POST") r.HandleFunc("/people/{id}", updatePerson).Methods("PUT") r.HandleFunc("/people/{id}", deletePerson).Methods("DELETE") http.Handle("/", r) http.ListenAndServe(":8080", nil) } func getPeople(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(people) } func getPerson(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] for _, p := range people { if p.ID == id { json.NewEncoder(w).Encode(p) return } } http.Error(w, "Person not found", http.StatusNotFound) } func createPerson(w http.ResponseWriter, r *http.Request) { var p Person json.NewDecoder(r.Body).Decode(&p) p.ID = len(people) + 1 people = append(people, p) json.NewEncoder(w).Encode(p) } func updatePerson(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] for i, p := range people { if p.ID == id { json.NewDecoder(r.Body).Decode(&p) people[i] = p json.NewEncoder(w).Encode(p) return } } http.Error(w, "Person not found", http.StatusNotFound) } func deletePerson(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] for i, p := range people { if p.ID == id { people = append(people[:i], people[i+1:]...) w.WriteHeader(http.StatusNoContent) return } } http.Error(w, "Person not found", http.StatusNotFound) }
以上是Golang 進字節求職攻略大全的詳細內容。更多資訊請關注PHP中文網其他相關文章!