Unable to declare fields of embedded struct in go [duplicate] - go

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.

Related

cannot use mapToPrint (variable of type map[string]CustomStruct) as type map[string]any - golang [duplicate]

This question already has an answer here:
Gomock cannot use type map[string]*mockFoo as map[string]foo
(1 answer)
Closed 4 months ago.
In my function I was receiving an argument that contained a map for which the value's type was any. I would have thought that any type could therefore be sent, but I got the following error when I tired to use map[string]CustomStruct:
cannot use mapToPrint (variable of type map[string]CustomStruct) as type map[string]any in argument to printMap.
If I create the map with type's value any, everything works, including the assignment of CustomStruct to map values.
Here is a reproducing example:
type CustomStruct struct {
name string
}
func main() {
mapToPrint := make(map[string]CustomStruct, 0)
mapToPrint["a"] = CustomStruct{"a"}
mapToPrint["b"] = CustomStruct{"b"}
printMap(mapToPrint)
}
func printMap(mapToPrint map[string]any) {
for key, value := range mapToPrint {
fmt.Println(key, value)
}
}
go.dev
As the go FAQ says here:
It is disallowed by the language specification because the two types do not have the same representation in memory.
As the FAQ suggests, simply copy the data from your map into a map with value type any, and send it like so to the function:
printableMap := make(map[string]any, len(mapToPrint))
for key, value := range mapToPrint {
printableMap[key] = value
}

Accessing a strut from another package [duplicate]

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.

Difference between the types of accessing pointer values in golang [duplicate]

This question already has answers here:
Are pointers dereferenced by default inside of methods?
(2 answers)
Closed 8 months ago.
Consider the below example
type Employee struct {
Firstname string
// other fields
}
func (e *Employee) SetName(name string) {
e.Firstname = name // type 1
(*e).firstName = name // type 2
}
What is the difference between type 1 and type 2 ways of accessing properties here? When should we use one over the other?
Type 1 is a shorthand for type 2. Use the shorthand notation.
Here's the quote from the specification:
if the type of x is a defined pointer type and (*x).f is a valid selector expression denoting a field (but not a method), x.f is shorthand for (*x).f.

Empty output while trying to convert a yaml data into a struct [duplicate]

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
}

Cannot assign struct field in nested map Golang [duplicate]

This question already has answers here:
Golang: I have a map of int to struct. Why can't I directly modify a field in a map value? [duplicate]
(1 answer)
Why do I get a "cannot assign" error when setting value to a struct as a value in a map? [duplicate]
(2 answers)
Cannot assign to struct field in a map
(5 answers)
Closed 4 years ago.
I have the next structure:
type filesDB struct {
Name string
Content []byte
}
type fileDB struct {
Files map[string]filesDB
}
Well, when I try to save data in JSON format with the maps, I try this:
First, I make a map var:
var filesDatabase = map[string]fileDB{}
Then, i try to insert data:
var f filesDB
fidb := map[string]filesDB{}
f.Name= filename
f.Content= encrypt(b, sUser[user].Cipher)
fidb[filename] = f
filesDatabase[user].Files = fidb
jsonDB, err := json.Marshal(filesDatabase)
So, when I try to run the script, I get the next error:
cannot assign to struct field filesDatabase[user].Files in map
How can I resolve this error? Thanks!

Resources