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!
Related
This question already has answers here:
Change values while iterating
(4 answers)
Range references instead values
(8 answers)
increment a struct var in a range loop [duplicate]
(1 answer)
How to return changed values of slice from function? [duplicate]
(2 answers)
Why can't I change the values in a range of type structure?
(2 answers)
Closed 3 days ago.
I am new to Go. I've encountered an issue with Go pointers and don't have an explanation for the unexpected behavior.
I am using using Go 1.20 on RedHat.
Here is my test case:
package main
import (
"fmt"
)
type FileStruct struct {
readyToSend int
}
func sendFile(fileStr *FileStruct) {
fileStr.readyToSend = 2
}
func readFileFeeder(filesArr []FileStruct) {
filesArr[0].readyToSend = 1
for _, file := range filesArr {
fmt.Printf("TYPE=%T\n", &file)
sendFile(&file)
fmt.Printf("After range readyToSend=%v\n", filesArr[0].readyToSend)
}
fmt.Printf("TYPE=%T\n", &filesArr[0])
sendFile(&filesArr[0])
}
func main() {
var file1 *FileStruct = new(FileStruct)
filesArr := make([]FileStruct, 1)
filesArr[0] = *file1
fmt.Printf("Before readyToSend=%v\n", filesArr[0].readyToSend)
readFileFeeder(filesArr)
fmt.Printf("After readyToSend=%v\n", filesArr[0].readyToSend)
}
output:
==\> go run test.go
Before readyToSend=0
TYPE=\*main.FileStruct
After range readyToSend=1
TYPE=\*main.FileStruct
After readyToSend=2
How come when I call sendFile(&file) inside the for loop the value of fileStr.readyToSend doesn't get updated?
When I call sendFile(&filesArr[0]) right after the for loop the value gets updated as expected.
I would expect that fileStr.readyToSend would get updated to 2 in both cases.
How would you update this sample code to make this work?
Thanks
This question already has answers here:
How can you get a reflect.Type instance of an struct without physically creating the struct?
(1 answer)
TypeOf without an instance and passing result to a func
(2 answers)
How to get Type representation from name via reflection?
(1 answer)
Closed 8 months ago.
I have this Go code:
type Op interface{}
type Op1 struct{}
type Op2 struct{}
I'm trying to map Types to int. Something like this (pseudo-code):
mapping := map[reflect.Type]int
mapping[Op1] = 20
mapping[Op2] = 30
I know this is possible by creating an instance of Op1/Op2 and call reflect.TypeOf:
op1 := Op1{}
op2 := Op1{}
mapping := map[reflect.Type]int
mapping[reflect.TypeOf(op1)] = 20
mapping[reflect.TypeOf(op2) = 30
but my question is if it's possible to do that "statically" without creating an object. Something like .class in Java.
This question already has answers here:
get value form nested map type map[string]interface{}
(2 answers)
Accessing Nested Map of Type map[string]interface{} in Golang
(2 answers)
How do you extract a value out of a golang map[string] string thats typed with interface{} [duplicate]
(1 answer)
Explain Type Assertions in Go
(3 answers)
how to access nested Json key values in Golang
(3 answers)
Closed last year.
I have this structure in golang:
type ScanView struct {
FileInput []ScanInput `json:"inputs"`
Report RegulaReport `json:"report"`
}
type FileInput struct {
Filepath string `json:"filepath"`
Resources map[string]map[string]interface{} `json:"resources"`
}
I need to assign values to the map depending on the key, I'm doing something like this:
for i, r := range output.Inputs {
if filepath.IsAbs(r.Filepath) {
relPath, err := filepath.Rel(inputDir, r.Filepath)
if err != nil {
return "", fmt.Errorf("some error log")
}
output.Inputs[i].Resources["_source_location"]["path"] = relPath -> here I need to assign the value for the _source_location key
output.Inputs[i].Resources["_filepath"][""] = relPath -> here I need to assign the value for the _filepath key
}
}
No pretty sure how can I achieve that for a map[string]map[string]interface{} in Golang. Thanks!
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:
Assigning value in Golang
(3 answers)
Closed 7 years ago.
I create a var of type
var RespData []ResponseData
type ResponseData struct {
DataType string
Component string
ParameterName string
ParameterValue string
TableValue *[]Rows
}
type TabRow struct {
ColName string
ColValue string
ColDataType string
}
type Rows *[]TabRow
I want to fill TableValue of type *[]Rows.
Can you please tell me with an example by assigning any values in the TableValue.
Slices are reference type (it is already a kind of pointer), so you don't need a pointer to a slice (*[]Rows).
You can use a slice of slices though TableValue []Rows, with Rows being a slice of pointers to TabRow: Rows []*TabRow.
tr11 := &TabRow{ColName: "cname11", ColValue: "cv11", ColDataType: "cd11"}
tr12 := &TabRow{ColName: "cname12", ColValue: "cv12", ColDataType: "cd12"}
row1 := Rows{tr11, tr12}
rd := &ResponseData{TableValue: []Rows{row1}}
fmt.Printf("%+v", rd )
See this example.