简体   繁体   中英

Go unknown field … in struct literal of type

I am trying to define routes based on a custom routes-struct (containing a Prefix and an array of Subroutes).

I found the code in a tutorial and it worked as expected. My goal is to also include a new attribute "IsSecure (bool)" in the struct so that I can later check if the Router should use a Middleware for the specific route prefix or not...

This is the file for struct:

var AppRoutes []RoutePrefix // used to append routes later in the main.go

type RoutePrefix struct {
    IsSecure bool // -> causes the problem
    Prefix string
    SubRoutes []Route
}

type Route struct {
    Name string
    Method string
    Pattern string
    HandlerFunc http.HandlerFunc
}

The problem now is that when I try to define a new RoutePrefix, Go returns the following error :

unknown field 'IsSecure' in struct literal of type router.RoutePrefix

If I remove the field "IsSecure" from the struct, everything works fine.

This is how I define the Routes:

var PostRoutes = router.RoutePrefix {
IsSecure:  true,
SubRoutes: []router.Route{
        {
            Name:        "CreatePost",
            Method:      "POST",
            Pattern:     "",
            HandlerFunc: CreatePostHandler,
        },
        {
            Name:        "DeletePost",
            Method:      "DELETE",
            Pattern:     "/{postId}",
            HandlerFunc: DeletePostHandler,
        },
    },
}

What could cause the problem? I already tried to nest "RoutePrefix" in another struct that contains "IsSecure" which didn't work either.

The problem was that I was unintentionally trying to override an already existing package containing this struct.

Changing the package solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM