构建简单crud(没有操作数据库)

go mod init movie_crud
go get "github.com/gorilla/mux"
package main 
import (
    "encoding/json"
    "fmt"
    "log"
    "math/rand"
    "net/http"
    "strconv" 
    "github.com/gorilla/mux"
) 
type Movie struct {
    ID       string    `json:"id"`
    Isbn     string    `json:"isbn"`
    Title    string    `json:"title"`
    Director *Director `json:"director"`
}
type Director struct {
    FirstName string `json:"firstname"`
    LastName  string `json:"lastname"`
} 
var movies []Movie 
func getMovies(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(movies)
} 
func getMovie(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(r)
    for _, item := range movies {
        if item.ID == params["id"] {
            json.NewEncoder(w).Encode(item)
            return
        }
    }
} 
func createMovie(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var movie Movie
    // 绑定json数据到movie结构体
    _ = json.NewDecoder(r.Body).Decode(&movie)
    movie.ID = strconv.Itoa(rand.Intn(100000000))
    movies = append(movies, movie)
    json.NewEncoder(w).Encode(movie)
}
func updateMovie(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(r)
    for index, item := range movies {
        if item.ID == params["id"] {
            // 从0到index 拼接 从index+1到尾
            movies = append(movies[:index], movies[index+1:]...)
            var movie Movie
            // 绑定json数据到movie结构体
            _ = json.NewDecoder(r.Body).Decode(&movie)
            movie.ID = strconv.Itoa(rand.Intn(100000000))
            movies = append(movies, movie)
            json.NewEncoder(w).Encode(movies)
        }
    }
}
func deleteMovie(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    params := mux.Vars(r)
    for index, item := range movies {
        if item.ID == params["id"] {
            // 从0到index 拼接 从index+1到尾
            movies = append(movies[:index], movies[index+1:]...)
            break
        }
    }
    json.NewEncoder(w).Encode(movies)
}
func main() {
    r := mux.NewRouter()
    movies = append(movies, Movie{ID: "1", Isbn: "438227", Title: "Movie One",
        Director: &Director{
            FirstName: "john", LastName: "doe",
        }})
    movies = append(movies, Movie{ID: "2", Isbn: "45445", Title: "Movie Two",
        Director: &Director{
            FirstName: "steve", LastName: "smith",
        }})
    r.HandleFunc("/movies", getMovies).Methods("GET")
    r.HandleFunc("/movies/{id}", getMovie).Methods("GET")
    r.HandleFunc("/movies", createMovie).Methods("POST")
    r.HandleFunc("/movies/{id}", updateMovie).Methods("PUT")
    r.HandleFunc("/movies/{id}", deleteMovie).Methods("DELETE")
    fmt.Println("listening in port: 8000")
    log.Fatal(http.ListenAndServe(":8000", r))
}

  目录