0

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,

Mr. E
  • 457
  • 1
  • 5
  • 20
  • 1
    Your `vars` type has no `append` method, and being a struct, what would it mean to "append"? You may want to start with the [Tour of Go](https://go.dev/tour/welcome/1) and the [Getting Started Tutorials](https://go.dev/doc/tutorial/getting-started) to get a handle on the basics of the language. – JimB Aug 14 '23 at 14:51
  • 1
    Before learning to use a framework like Gin you should gain a basic understanding of Go itself. As JimB pointed out the Go Tour website is the first thing you should walk through and ensure you understand all the concepts it covers. – Kurtis Rader Aug 14 '23 at 21:13
  • Thanks all.... Most helpful..... But, I am beginning to like gin.... – Mr. E Aug 15 '23 at 09:51

1 Answers1

1

Here is what I was going for....

package main

import (
    "fmt"
    "os"
    "strings"
    "github.com/gin-gonic/gin"
    "encoding/json"
    "net/http"
)

func main() {
    r := gin.Default()
    r.GET("/vars", func(c *gin.Context) {
        readVars := make(map[string]string)
        for _, env := range os.Environ() {
            envPair := strings.SplitN(env, "=", 2)
            key := envPair[0]
            value := envPair[1] 
                    
            readVars[key] = value
        }
        json, err := json.MarshalIndent(readVars, "", "\t")
        if err != nil {
            fmt.Println("error:", err)
        }
        c.Data(http.StatusOK, "application/json", json)
    })
    r.Run(":3000")
}

and it give a nice tabbed output at http://localhost:3000/vars

{
    "ALLUSERSPROFILE": "C:\\ProgramData",
    "ProgramData": "C:\\ProgramData",
    "ProgramFiles": "C:\\Program Files",
    "ProgramFiles(x86)": "C:\\Program Files (x86)",
    "ProgramW6432": "C:\\Program Files",
    "SystemDrive": "C:",
    "SystemRoot": "C:\\WINDOWS",
    "windir": "C:\\WINDOWS"
}
Mr. E
  • 457
  • 1
  • 5
  • 20