drutjkpsr67393592 2015-01-11 13:36
浏览 17
已采纳

Golang类型别名(类型SampleType otherType!== otherType)?

I am building a simple router for which the code is below.

I have a callback that takes type Res as a parameter. Initially I was under the impression that type Res http.ResponseWriter would simply mean that Res is an alias for http.ResponseWriter, as passing http.ResponseWriter into the callback with a Res param worked fine.

I later decided I wanted to wrap my own functionality around the response provided to the callback, and so changed the type Res http.ResponseWriter to type Res response.Response - response being my own package (please look at what is commented out in the source below). After doing so I get "Cannot use newResponse (type response.Response) as type Res in argument to urlCallback"

I looked at the http.ResponseWriter source code and noticed it is an interface whereas my response.Response is a struct, is this where the problem lies?

Does setting a type as another type does not necessarily alias it? And if not, then why did passing in http.ResponseWriter for type Res work initially?

package Router

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

// type Res response.Response
type Res http.ResponseWriter
type Req *http.Request

type RouteMap map[string]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)
    }

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

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

func (router Router) determineHandler(w http.ResponseWriter, r *http.Request) {
    methodMap := router.Methods[r.Method]
    urlCallback := methodMap[r.URL.Path]

    // newResponse := response.NewResponse(w)

    if(urlCallback != nil) {
        // urlCallback(newResponse, r)
        urlCallback(w, r)
    }
}

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)
    }
}

The commented out code describes the issue im having/what im trying to do

  • 写回答

1条回答 默认 最新

  • dpjo15650 2015-01-11 15:21
    关注

    Doing

    type Res response.Response
    

    you define new type, which is not interchangeable with original

    to convert you should do

    newResponse := Res(response.NewResponse(w))
    

    when you do

    type Res http.ResponseWriter
    

    your type Res is an interface and http.ResponseWriter implement it, so can be used as arg in func where Res wanted

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?