GoRestAPI

/ / GoRestAPI
	
package main
import (
   "encoding/json"
   "log"
   "net/http"
   "github.com/gorilla/mux"
)
type Employee struct {
   ID        string   'json:"id,omitempty"'
   Firstname string   'json:"firstname,omitempty"'
   Lastname  string   'json:"lastname,omitempty"'
   Address   *Address 'json:"address,omitempty"'
}
type Address struct {
   City  string 'json:"city,omitempty"'
   State string 'json:"state,omitempty"'
}
var emp []Employee
func GetEmpIdEndpoint(w http.ResponseWriter, req *http.Request) {
   params := mux.Vars(req)
   for _, item := range emp {
      if item.ID == params["id"] {
         json.NewEncoder(w).Encode(item)
         return
      }
   }
   json.NewEncoder(w).Encode(&Employee{})
}
func GetEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {
   json.NewEncoder(w).Encode(emp)
}
func CreateEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {
   params := mux.Vars(req)
   var person Employee
   _ = json.NewDecoder(req.Body).Decode(&person)
   person.ID = params["id"]
   emp = append(emp, person)
   json.NewEncoder(w).Encode(emp)
}
func DeleteEmployeeEndpoint(w http.ResponseWriter, req *http.Request) {
   params := mux.Vars(req)
   for index, item := range emp {
      if item.ID == params["id"] {
         emp = append(emp[:index], emp[index+1:]...)
         break
      }
   }
   json.NewEncoder(w).Encode(emp)
}
func main() {
   router := mux.NewRouter()
   emp = append(emp, Employee{ID: "1", Firstname: "Nic", Lastname: "Raboy", 
   Address: &Address{City: "Dublin", State: "CA"}})
   emp = append(emp, Employee{ID: "2", Firstname: "Maria", Lastname: "Raboy"})
   router.HandleFunc("/employee", GetEmployeeEndpoint).Methods("GET")
   router.HandleFunc("/employee/{id}", GetEmpIdEndpoint).Methods("GET")
   router.HandleFunc("/employee/{id}", CreateEmployeeEndpoint).Methods("POST")
   router.HandleFunc("/employee/{id}", DeleteEmployeeEndpoint).Methods("DELETE")
   log.Fatal(http.ListenAndServe(":12345", router))
}

输出:

You can check the output by installing postman extension for chrome browser

Method--> Get,

 url -->http://localhost:8080/employee
Response : 
[{"id":"1","firstname":"James","lastname":"Learnfkson","address":{"city":"Hoseynabad","state":"Kavir"}}
,{"id":"2","firstname":"Sarah","lastname":"Taylor","address":{"city":"Kamenka","state":"Vyborgsky"}}]

Method--> GET,

 url -->http://localhost:8080/employee/1
Response : 
{"id":"1","firstname":"James","lastname":"Learnfkson","address":
"city":"Hoseynabad","state":"Kavir"}}

Method--> POST,

url -->http://localhost:8080/employee/3
Response : 
[{"id":"1","firstname":"James","lastname":"Learnfkson","address":{"city":"Hoseynabad","state":"Kavir"}},
{"id":"2","firstname":"Sarah","lastname":"Taylor","address":{"city":"Kamenka","state":"Vyborgsky"}},
{"id":"3","firstname":"Roger","lastname":"Ponting","address":{"city":"San Pedro","state":"LA"}}]

Method--> DELETE,

url -->http://localhost:8080/employee/2
Response : 
[{"id":"1","firstname":"James","lastname":"Learnfkson","address":{"city":"Hoseynabad","state":"Kavir"}},
{"id":"3","firstname":"Roger","lastname":"Ponting","address":{"city":"San Pedro","state":"LA"}}]

祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)

精选教程推荐

👇 以下精选教程可能对您有帮助,拓展您的技术视野

AI音视频创作入门课 -〔唐子轩〕

物联网开发实战 -〔郭朝斌〕

跟月影学可视化 -〔月影〕

正则表达式入门课 -〔涂伟忠〕

检索技术核心20讲 -〔陈东〕

OpenResty从入门到实战 -〔温铭〕

Python核心技术与实战 -〔景霄〕

许式伟的架构课 -〔许式伟〕

iOS开发高手课 -〔戴铭〕

📝 好记忆不如烂笔头,留下您的学习笔记吧!

暂无学习笔记,成为第一个分享的人吧!

您的笔记将帮助成千上万的学习者