dongle7553 2015-01-10 03:59
浏览 44
已采纳

golang-地图中的功能为<nil>

I am trying to build a simple router in GO, I have a get method on a struct that should be passing a callback to the Get route map with the url as the key, it seems that fmt.Println(urlCallback) is returning a nil value and causes a runtime panic if i tried to call it, coming from a javascript background I am only just coming to grips with pointers and the like and feel it may have something to do with this,if someone could tell me why the passed func is nil that would be great.

Here is my "Router" package.

package Router

import (
    "fmt"
    "net/http"
    "net/url"
    "log"
)

type Res http.ResponseWriter
type Req *http.Request

type RouteMap map[*url.URL]func(Res, Req) 
type MethodMap map[string]RouteMap

type Router struct {
    Methods MethodMap
}

func (router *Router) Get(urlString string, callback func(Res, Req)) {
    parsedUrl, err := url.Parse(urlString)

    if(err != nil) {
        panic(err)
    }

    fmt.Println(parsedUrl)

    router.Methods["GET"][parsedUrl] = callback
}

func (router *Router) initMaps() {
    router.Methods = MethodMap{}
    router.Methods["GET"] = RouteMap{}
}

func (router Router) determineHandler(res http.ResponseWriter, req *http.Request) {
    fmt.Println(req.URL)
    fmt.Println(req.Method)

    methodMap := router.Methods[req.Method]
    urlCallback := methodMap[req.URL]

    fmt.Println(methodMap)
    fmt.Println(urlCallback)
}

func (router Router) Serve(host string, port string) {
    fullHost := host + ":" + port

    fmt.Println("Router is now serving to:" + fullHost)
    http.HandleFunc("/", router.determineHandler)

    err := http.ListenAndServe(fullHost, nil)

    if err == nil {
        fmt.Println("Router is now serving to:" + fullHost)
    } else {
        fmt.Println("An error occurred")
        log.Fatal(err)
    }
}


func NewRouter() Router {
    newRouter := Router{}
    newRouter.initMaps()

    return newRouter
}

and my main.

package main

import (
    "./router"
    "fmt"
)

func main() {
    router := Router.NewRouter()

    router.Get("/test", func(Router.Res, Router.Req) {
        fmt.Println("In test woohooo!")
    })

    router.Serve("localhost", "8888")
}
  • 写回答

1条回答 默认 最新

  • dongque1462 2015-01-10 05:41
    关注

    You are using *URL.url objects for map keys. Since two different objects won't be the same, you can't access the key for that path again. It's panicking because

    urlCallback := methodMap[req.URL]
    

    isn't an existing key, and so you're accessing a nil value. What you probably want to do in this case is use the Path property of the URL.url object.

    So you'd have:

    type RouteMap map[string]func(Res, Req)
    

    In Get():

    router.Methods["GET"][parsedUrl.Path] = callback
    

    For determineRouter(), you can do this:

    urlCallback, exists := methodMap[req.URL.Path]
    if exists != false {
        urlCallback(res, req)
    }
    

    This adds a check to see if the key exists before it tries to call it.

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

报告相同问题?

悬赏问题

  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥15 C++数组中找第二小的数字程序纠错