This question already has answers here:
Invoking struct function gives "cannot refer to unexported field or method"
(2 answers)
Closed 2 years ago.
Please help to understand, how I can return from function not only values from structure but with their names?
Example my arg.go
package flags
import (
"flag"
)
type FlagsStruct struct {
argTest1 string
argTest2 string
}
func GetInitFlags() *FlagsStruct {
Flags := new(FlagsStruct)
flag.StringVar(&Flags.argTest1, "test1", "test1", "c")
flag.StringVar(&Flags.argTest2, "test2", "test2", "t")
flag.Parse()
return Flags
}
It's working only with keys, for example in my main function I trying to print and it's working:
fmt.Print(*inputFlags)
{test1 test2}
But how I can pass taht can print something like this?
fmt.Printf(inputFlags.argTest2)
./main.go:25:24: inputFlags.argTest2 undefined (cannot refer to
unexported field or method argTest2)
Make your argument names start with a capital letter (ArgTest2). See this question for more details.
Related
This question already has answers here:
Private fields and methods for a struct
(6 answers)
Closed 6 months ago.
I have the following modules
package apack
type Something structs{
a string
b string
}
var FullList []Something
func Complete() []Something {
FullList = append(FullList, Something{
a: 'first',
b: 'second'
})
return FullList
}
Now the following main
import "something/apack"
func main() {
re = apack.Complete()
for _,s := range re {
s1 := apack.Something(s)
fmt.Println(s1)
}
}
when I run it I get the following:
{first second}
but if I do something like
fmt.Println(s1.a)
I get the following error:
./main.go:70:19: s1.a undefined (type apack.Something has no field or method a)
Is it possible to be able to access structs from another package?
I think map should work, just unsure how for this case.
Thanks
Yes it's possible, just make the fields of your struct (that you want to access from the other package) exported:
type Something struct {
A string
b string
}
Then you can access all the exported fields from the struct so fmt.Println(s1.A) in the other package code will now work, but fmt.Println(s1.b) will not work as b is still an unexported field.
Also, here is a very simple lesson from A Tour of Go (which I also recommend as a whole) about exported names.
This question already has answers here:
Struct field reverts [duplicate]
(1 answer)
How to set and get fields in struct's method
(3 answers)
Assign a new value to a struct field
(2 answers)
object variable is not getting updated in Golang [duplicate]
(1 answer)
Unable to set the property of a struct [duplicate]
(1 answer)
Closed 1 year ago.
This is my second day of learning Go and I'm trying to better understand structs and methods, so apologies in advance if this questions is a bit basic. Why does this program print the edited data inside the method, but by the time it gets back to main, what's printed is the original data passed to the struct?
package main
import ( "fmt" )
type Person struct{
firstName string
lastName string
}
func (p Person) updateName(newName string) string{
p.firstName = newName
fmt.Println(p.firstName, p.lastName) //prints "Jane Doe"
return newName + " " + p.lastName
}
func main() {
p := Person{"John", "Doe"}
p.updateName("Jane")
fmt.Println(p) //prints "{John Doe}"
}
Thanks in advance!
This question already has answers here:
Golang embedded struct type
(3 answers)
Closed 2 years ago.
Within a go package named kubesecretsPkg I declare the following two struct types:
type KubesecretsOpts struct {
FullPathToRepo string
FullPathToApp string
}
type KubesecretsSetOpts struct {
KubesecretsOpts
Variable string
Value string
}
I am trying to initialise an outer (KubesecretsSetOpts) in another package as follows:
kSetOpts := kubesecretsPkg.KubesecretsSetOpts{
kubesecretsPkg.KubesecretsOpts: {
FullPathToRepo: fullPathToRepo,
FullPathToApp: fullPathToApp,
},
Variable: variable,
Value: value,
}
this fails as follows:
Why am I not able to directly initialise an embedded struct?
The right syntax would be
kSetOpts := kubesecretsPkg.KubesecretsSetOpts{
KubesecretsOpts: kubesecretsPkg.KubesecretsOpts{
FullPathToRepo: fullPathToRepo,
FullPathToApp: fullPathToApp,
},
Variable: variable,
Value: value,
}
name of the field in the struct is "KubesecretsOpts" even in this scope however the type of the field is kubesecretsPkg.KubesecretsOpts. You are getting "invalid field name" error due to former fact in this scope.
This question already has answers here:
How to access command-line arguments passed to a Go program?
(6 answers)
Closed 2 years ago.
I would like to log all the flags(cmdline args) passed to a program at startup. How do I do this? Currently the program uses flag package to read the flags into the program .
If you're using the flag package, there is the concept of a "flag" which is a defined command line argument i.e.
name := flag.String("name", "default", "some name")
And also the concept of an "arg" which is an undefined command line argument (i.e. not a flag).
You can get the list of args with flag.Args() which returns string[].
There doesn't seem to be a way to get the list of flags. There are Visit functions. You could use VisitAll that takes a function to execute on each flag:
flag.VisitAll(func(f *flag.Flag) {
fmt.Printf("%s: %s\n", f.Name, f.Value)
})
You can use os.Args, which is a slice of strings, from the os package.
E.g:
package main
import (
"log"
"os"
)
func main() {
log.Println(os.Args)
}
This question already has answers here:
Go Unmarshaling YAML into struct
(2 answers)
Closed 4 years ago.
Im trying to convert a yaml data into a struct and print it. The output I get for this program is empty.
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type example struct {
variable1 string
variable2 string
}
func main() {
var a example
yaml.Unmarshal([]byte("variable1: asd\nvariable2: sdcs"), &a)
fmt.Println(a.variable1)
}
The documentation for Unmarshal states that
Struct fields are only unmarshalled if they are exported (have an upper case first letter) and are unmarshalled using the field name lowercased as the default key.
So capitalizing your struct elements is the right thing to do.
type example struct {
Variable1 string
Variable2 string
}