您的位置:首页 >Golang 进字节求职攻略大全
发布于2025-05-11 阅读(0)
扫一扫,手机访问
字节 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)
}
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9