[go-nuts] Re: global var available in different files (same package)
On Friday, June 7, 2019 at 6:07:09 PM UTC+2, jake...@gmail.com wrote: > > > > It is unclear what the problem is. Global variables will be accessible > from any file that is part of package "main". As you said, you could > convert the strings in main(), then put them in some global variables, for > use by you other functions. That way you wold not have to be passing them > around. Or am I missing something? > yes, I understand it's unclear, if you do not fully understand something it's difficult to put it into words. What I mean is that if I convert the var url URL to a string type variable (in this case I need to pass the uri around I get from the cli flag), and this conversion takes places forcefully after parsing the cli args, in func main(), then the new string variable is not passed to the second file. I need to pass it as an argument to the function I want to execute in the second file. so first file: func main() { kingpin.MustParse(app.Parse(os.Args[1:])) uri := *url fmt.Println(uri.String(), "from main.go") whatever() } second file: func whatever() { fmt.Println(uri.String(), "from ww.go") } $ go run *.go -u kk # command-line-arguments ./ww.go:10:14: undefined: uri But the url var is available, I just need to unpack it everytime, which is tedious. I hope I made it clearer now. It's not really an issue but a question to find a better way if possible ;-) Thanks, regards, Natxo -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/06fe42bf-2f6a-47d8-928f-106476d277e3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] global var available in different files (same package)
hi, I'd like to define a couple of global variables in a small cli app. The app has a few files but it's not large. What I'd like is to use cli flags to define options, and those options should be available for all the functions. I quite like the fact that the content of the variables is checked for correctnes. Code (using kingpin): ==kinpingtest.go= package main import ( "gopkg.in/alecthomas/kingpin.v2" "os" ) var ( app= kingpin.New("shittytest", "shitty kingpin test") url= app.Flag("url", "url for app").Required().Short('u').URL() domain = app.Flag("domain", "domain for app").Short('d').Required().String() ) func main() { kingpin.MustParse(app.Parse(os.Args[1:])) whatever() } end kingpintest.go and second file: =otherfile.go package main import ( "fmt" ) func whatever() { u := *url fmt.Println(u.String() + "/that") fmt.Println(*domain) } ===endotherfile.go=== And quit unsurprisingly, running it: $ go run *.go --url http://this.com --domain kk.kk http://this.com/that kk.kk The 'problem' I have is that I cannot use the variables directly and have to convert them to strings first, and as far as I can see I cannot do that globally because the kingpin arguments get parsed in main(). I see one possible solution, and that is to convert the vars to strings in main() and pass those as arguments to funcs called from main(). Are there better options for what I want to achieve (certainly must be, I am just a go newbie)? Thanks in advance. Regards, Natxo -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/795cfc24-ace3-4c31-b898-f0f75ed25a60%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] trouble passing an argument to a script
On Tue, Feb 26, 2019 at 10:58 AM Natxo Asenjo wrote: > > hi, > > On Tuesday, February 26, 2019 at 10:51:28 AM UTC+1, Sebastien Binet wrote: >> >> from the compilation error, it would seem kingpin.Flag...ExistingFile() >> returns a *string, not a string. >> so these lines: >> fmt.Printf("%v, %s, %T\n", config, config, config) >> cfg, err := ini.Load(config) >> should be replaced with: >> fmt.Printf("%v, %s, %T\n", *config, *config, *config) >> cfg, err := ini.Load(*config) >> >> Thanks! > > I had indeed already tried that but alas: > > $ go run kk.go --config api.conf > , , string > open : no such file or directory > exit status 1 > > after dereferencing config, it appears to be empty. > > it appears that defining the config variable and the other variables globally does not work. If I define the url/api/user/pwd in main(), then I can use the config variable as expected (indeed with a pointer *config). Thanks for the tip. -- regards, natxo -- 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.
Re: [go-nuts] trouble passing an argument to a script
hi, On Tuesday, February 26, 2019 at 10:51:28 AM UTC+1, Sebastien Binet wrote: > > from the compilation error, it would seem kingpin.Flag...ExistingFile() > returns a *string, not a string. > so these lines: > fmt.Printf("%v, %s, %T\n", config, config, config) > cfg, err := ini.Load(config) > should be replaced with: > fmt.Printf("%v, %s, %T\n", *config, *config, *config) > cfg, err := ini.Load(*config) > > Thanks! I had indeed already tried that but alas: $ go run kk.go --config api.conf , , string open : no such file or directory exit status 1 after dereferencing config, it appears to be empty. Scratching my head ... -- 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.
[go-nuts] trouble passing an argument to a script
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 0xc547e0, %!s(*string=0xc547e0), *string error parsing data source: unknown type '%!s(*string=0xc8ceb0)' exit status 1 This must be pretty basic, but I'm stuck :( Any tips welcome. Thanks in advance. -- regards, Natxo -- 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.
Re: [go-nuts] [crypto/x509] SystemCertPool() print subjects of ca certs
On Wednesday, October 31, 2018 at 10:46:41 PM UTC+1, golan...@geek1.de wrote: > > Hy Natxo, > > check out the pkix [0] and asn1 [1] packages. You can try to parse the > DER encoded subject into RDNSequence. > > [0]: https://golang.org/pkg/crypto/x509/pkix/#RDNSequence > [1]: https://golang.org/pkg/encoding/asn1/#Unmarshal > > Spoiler: > > This should work: > > package main > > import "fmt" > import "crypto/x509" > import "encoding/asn1" > import "crypto/x509/pkix" > > func main() { > store, err := x509.SystemCertPool() > if err != nil { > panic(err) > } > subjects := store.Subjects() > for _, rawSubject := range subjects { > var subject pkix.RDNSequence > if _, err := asn1.Unmarshal(rawSubject, ); err != nil { > panic(err) > } > fmt.Printf("%+v\n", subject) > } > } > > The unmarshal code is borrowed from https://stackoverflow.com/a/50640119 > > Cheers, > Michael > > Hi, it works beautifully. Thanks a lot. -- regards, Natxo -- 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.
[go-nuts] [crypto/x509] SystemCertPool() print subjects of ca certs
hi, as a learning exercise I would lke to loop through the system's certificate authorities store, and get the subjects of each certificate authority. I have this code: package main import "fmt" import "crypto/x509" func main() { store, err := x509.SystemCertPool() if err != nil { panic(err) } fmt.Printf("%T\n", store) subjects := store.Subjects() fmt.Printf("%T\n", subjects) for _, v := range subjects { fmt.Printf("%T\n", v) fmt.Println(string(v)) } } But I am getting a lot of gibberish. There should be a better way to do this, but I obviously do not know it. Thanks for any pointers. -- regards, Natxo -- 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.