Recently started messing about with go and gin. I am attempting to have gin output the environmental variables set in the environment. However, I'm a little confused with the error I am getting.
PS .\Desktop\go-env> go run .\vars.go
# command-line-arguments
.\vars.go:28:11: newVars.append undefined (type vars has no field or method append)
.\vars.go:31:16: cannot use newVars (variable of type vars) as string value in argument to c.String
the code is simple enough, but it seems to be complaining about my struct. Anyone know what the deal is with that?
package main
import (
"os"
"strings"
"github.com/gin-gonic/gin"
)
type vars struct {
key string `json:"key"`
value string `json:"value"`
}
func postVars(c *gin.Context) {
var readVars vars
if err := c.BindJSON(&readVars); err != nil {
return
}
for _, env := range os.Environ() {
envPair := strings.SplitN(env, "=", 2)
key := envPair[0]
value := envPair[1]
readVars.append(key, value)
}
c.String(200, readVars)
}
func main() {
router := gin.Default()
router.GET("/vars", postVars)
router.Run(":8080")
}
Ta,