Let's say I have a type that's a struct like so:
type Authorization struct {
Username string
Password string
Handler func(http.HandlerFunc) http.HandlerFunc
}
And I have an array of these:
type Authorizations map[string]*Authorization
I want to be able to do something like this:
var auth = Authorizations{
"test": *Authorization{
"someusername",
"somepassword",
self.BasicAuth,
},
}
Assume that self.BasicAuth (which obviously doesn't work) is a method on the Authorization type. What is the syntactically correct way to do this?
You can't refer to a value inside its own declaration. You need to initialize the value first, then you can assign the method you want to use to Handler.
testAuth := &Authorization{
Username: "someusername",
Password: "somepassword",
}
testAuth.Handler = testAuth.HandleFunc
auths := Authorizations{
"test": testAuth,
}