2019. február 26., kedd 10:46:44 UTC+1 időpontban Natxo Asenjo a következőt 
írta:
>
> hi,
>
> I am writing a script that accepts arguments and flags (using kingpin) but 
> am failing miserably so far. One of the arguments is a file name, which 
> should be then loaded by an ini library (file is in ini format).
>
> If I hard code the file name in the script, it works, but 
>
> Code:
>
> =====================start ====================================
> package main
>
> import (
>     "fmt"
>     "gopkg.in/alecthomas/kingpin.v2"
>     "gopkg.in/ini.v1"
>     "os"
> )
>
> var (
>     config              = kingpin.Flag("config", "configuration 
> file").Short('c').Required().ExistingFile()
>     url, api, user, pwd = parse_config()
> )
>
> func init() {
>     kingpin.Parse()
> }
>
> func main() {
>     fmt.Println("yay")
> }
>
> func parse_config() (string, string, string, string) {
>     //cfg, err := ini.Load("api.conf")
>     fmt.Printf("%v, %s, %T\n", config, config, config)
>     cfg, err := ini.Load(config)
>     if err != nil {
>         fmt.Println(err)
>         os.Exit(1)
>     }
>
>     url := cfg.Section("").Key("url").String()
>     api := cfg.Section("").Key("api").String()
>     user := cfg.Section("").Key("user").String()
>     pwd := cfg.Section("").Key("password").String()
>
>     return url, api, user, pwd
> }
>
> =====================end====================================
> and when running it:
>
> $ go run yetanother.go --config api.conf 
> 0xc0000547e0, %!s(*string=0xc0000547e0), *string
> error parsing data source: unknown type '%!s(*string=0xc00008ceb0)'
> exit status 1
>
> This must be pretty basic, but I'm stuck :(
>
> Any tips welcome.
>
> Thanks in advance.
>
> -- 
> regards,
> Natxo
>
>
>
https://godoc.org/gopkg.in/alecthomas/kingpin.v2#FlagClause.ExistingFile 
returns a *string (a pointer to a string),  
https://godoc.org/gopkg.in/ini.v1#Load wants a string - so dereference it:

    cfg, err := ini.Load(*config)


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to