douzhaobo6488 2018-02-28 18:30
浏览 69
已采纳

Golang Mux路由器处理程序函数参数

I was trying to set up a CRUD http API using gorilla-mux library.

I followed a youtube tutorial Implementation is below: -

package main

import (
    "github.com/gorilla/mux"
    "net/http"
    "log"
)

type Book struct {
    Id     string  `json:"id"`
    Isbn   string  `json:"isbn"`
    Title  string  `json:"title"`
    Author *Author `json:"author"`
}

type Author struct {
    Firstname string `json:"firstname"`
    Lastname  string `json:"lastname"`
}

// Get all books
func getBooks(w http.ResponseWriter, r *http.Response) {

}

// Get single book
func getBook(w http.ResponseWriter, r *http.Response) {

}

// Create a book
func createBook(w http.ResponseWriter, r *http.Response) {

}

// Update a book
func updateBook(w http.ResponseWriter, r *http.Response) {

}

// Delete a book
func deleteBook(w http.ResponseWriter, r *http.Response) {

}

func main() {
    r := mux.NewRouter()

    r.HandleFunc("/api/books", getBooks).Methods("GET")
    r.HandleFunc("/api/book/{id}", getBook).Methods("GET")
    r.HandleFunc("/api/book", createBook).Methods("POST")
    r.HandleFunc("/api/book/{id}", updateBook).Methods("PUT")
    r.HandleFunc("/api/book/{id}", deleteBook).Methods("DELETE")

    r.Path("/api/books").Methods("GET").HandlerFunc(getBooks)

    log.Fatal(http.ListenAndServe(":8000", r))
}

When I do go build on this file, I get below compilation errors -

./main.go:49:15: cannot use getBooks (type func(http.ResponseWriter, *http.Response)) as type func(http.ResponseWriter, *http.Request) in argument to r.HandleFunc ./main.go:50:15: cannot use getBook (type func(http.ResponseWriter, *http.Response)) as type func(http.ResponseWriter, *http.Request) in argument to r.HandleFunc ./main.go:51:15: cannot use createBook (type func(http.ResponseWriter, *http.Response)) as type func(http.ResponseWriter, *http.Request) in argument to r.HandleFunc ./main.go:52:15: cannot use updateBook (type func(http.ResponseWriter, *http.Response)) as type func(http.ResponseWriter, *http.Request) in argument to r.HandleFunc ./main.go:53:15: cannot use deleteBook (type func(http.ResponseWriter, *http.Response)) as type func(http.ResponseWriter, *http.Request) in argument to r.HandleFunc

What did I do wrong? What did I miss here? In the tutorial, he was able to build and run the file.

  • 写回答

1条回答 默认 最新

  • doumu5023 2018-02-28 18:35
    关注

    HanldeFunc type of function takes two paramters you are passing it wrong.

    // Get all books
    func getBooks(w http.ResponseWriter, r *http.Response) {
    
    }
    

    It should be *http.Request not *http.Response

    // Get all books
    func getBooks(w http.ResponseWriter, r *http.Request) {
    
    }
    

    Checkout on Go Playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误