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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看