字节 Go 郎求职攻略:简历准备:突出 go 经验和技能,量化项目成果。笔试复习:刷算法题,掌握 go 基础和并发特性。面试准备:深入理解 go,了解字节技术栈,准备项目经历和算法题。实
字节 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)
}
--结束END--
本文标题: Golang 进字节求职攻略大全
本文链接: https://www.lsjlt.com/news/597569.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0