I have this:
methods := [...]string{"POST", "PUT"}
router.HandleFunc(h.makeRegisterNewUser("/api/v1/register", v)).Methods("POST", "PUT")
that works except methods
is unused. If I try this:
methods := [...]string{"POST", "PUT"}
router.HandleFunc(h.makeRegisterNewUser("/api/v1/register", v)).Methods(methods...)
I get this error:
cannot use methods (type [2]string) as type []string in argument to router.HandleFunc(h.makeRegisterNewUser("/api/v1/register", v)).Methods
I can't figure this one out
You don't need the ...
in methods := [...]string{"POST", "PUT"}
methods := []string{"POST", "PUT"}
Edit: Arrays are slightly different from slices. The [...]
notation creates an array while a variadic function like Methods()
accepts a slice. If you really need an array you can take a slice of it using [:]
:
router.HandleFunc(h.makeRegisterNewUser("/api/v1/register", v)).Methods(methods[:]...)
The confusion arises from little bit different naming in Go and other languages. The data structure that resembles an array with different length and is called as "array", "vector", "list" in miscellaneous languages in Go is called "slice". And "array" in Go is another thing - a structure of fixed length.