-1

I'm trying to read in user CLI settings in one package and pass them on to another package. I've learned about private and public methods/attributes but can't seem to access them and I keep getting 'undefined' error message when building the app

main.go

package main

import (
    "fmt"
    "sun/argparser"
)


func main(){
    fmt.Println("Running script..")
    
    args := argparser.GetFlags()
    fmt.Printf("%+v\n", args)
    fmt.Println(args.Wallpaper)
}

argparser.go

package argparser

import (
    "log"
    "flag"
)

type Args struct {
    Wallpaper string
}

func GetFlags() Args {

    wallpaper := flag.String("wallpaper", "", "some help msg")
    flag.Parse()

    args := Args{}
    args.Wallpaper = string(*wallpaper)

    if args.Wallpaper == "" {
        log.Fatal("Need to provide valid wallpaper path")
    }

    return args
}

And when I try to build it it returns:

./main.go:15:19: args.Wallpaper undefined (type argparser.Args has no field or method Wallpaper)
michal-ko
  • 397
  • 4
  • 12
  • 2
    You're probably running an older version of the code, one which didn't have or exported the wallpaper field. What's the output of `fmt.Printf("%+v\n", args)`? You can see [here](https://go.dev/play/p/ai0a3uqRzEp) that if `Args` truly has a field called `Wallpaper` then it would be shown in the output of the `fmt.Printf("%+v\n" ...)` call. If it's not in the output then it's also not in the code. – mkopriva Aug 22 '23 at 15:04
  • 1
    You're right. That indeed was an issue – michal-ko Aug 22 '23 at 18:32

0 Answers0