Created
March 9, 2018 09:05
-
-
Save swanandvk/aa2cb52c56ed76f8f4d8f4f045ccf94d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // array of users | |
| var users []User | |
| func GetUserById(w http.ResponseWriter,req *http.Request){ | |
| params:=mux.Vars(req) | |
| for _,item := range users{ | |
| if item.ID==params["id"]{ | |
| json.NewEncoder(w).Encode(item) | |
| return | |
| } | |
| } | |
| // if not found return empty object with User structure | |
| json.NewEncoder(w).Encode(&User{}) | |
| } | |
| func CreateUser(w http.ResponseWriter,req *http.Request){ | |
| params :=mux.Vars(req) | |
| var user User | |
| _= json.NewDecoder(req.Body).Decode(&user) | |
| user.ID=params["id"] | |
| users=append(users,user) | |
| json.NewEncoder(w).Encode(users) | |
| } | |
| func GetUsers(w http.ResponseWriter,req *http.Request){ | |
| json.NewEncoder(w).Encode(users) | |
| } | |
| func DeleteUser(w http.ResponseWriter,req *http.Request){ | |
| params := mux.Vars(req) | |
| for index,item :=range users{ | |
| if item.ID==params["id"]{ | |
| users =append(users[:index],users[index+1:]...) | |
| break | |
| } | |
| } | |
| json.NewEncoder(w).Encode(users) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment