This question already has answers here:
How to convert interface{} to string?
(4 answers)
Closed 1 year ago.
below is my code
func main() {
var a interface{}
b := make(map[string]interface{})
b["mac_addr"] = "fa:16:3e:ba:95:bd"
b["type"] = "fixed"
b["addr"] = "1.1.1.1"
a = b
fmt.Println(a)
}
a gives me output
map[addr:1.1.1.1 mac_addr:fa:16:3e:ba:95:bd type:fixed]
issue is how do I access value of addr from a
use assert
val,ok:=a.(map[string]interface{})
if ok {
fmt.println(val["addr"])
}
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:
Generic Structs with Go
(1 answer)
Go error: cannot use generic type without instantiation
(2 answers)
Closed 10 months ago.
Here is the part of Go code I am reading:
func main() {
var m = map[int]string{1: "2", 2: "4", 4: "8"}
fmt.Println("keys m:", MapKeys(m))
_ = MapKeys[int, string](m)
**lst := List[int]{}**
lst.Push(10)
lst.Push(13)
lst.Push(23)
fmt.Println("list:", lst.GetAll())
}
Anyone can explain what is code below defined ?
lst := List[int]{}
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 Operator Overloading
(1 answer)
Why can't I append to a slice that's the property of a struct in golang?
(1 answer)
Closed 5 years ago.
When I tried to add a new method to an aliased type, append method not works.
package main
import (
"fmt"
)
type Strings []string
func (ss Strings) Add(s string) {
ss = append(ss, s)
}
func main() {
ss := make(Strings, 0)
ss = append(ss, "haha", "h3h3")
fmt.Println(ss) // got [haha h3h3]
ss.Add("lala")
fmt.Println(ss) // also got [haha h3h3], and why ?
}
Why doesn't "lala" get appended to ss?
This question already has answers here:
How to check if a map contains a key in Go?
(11 answers)
Closed 7 years ago.
The following code yields true. So I'm wondering for map[string]string in Golang, is there a way to differentiate empty string and nothing?
package main
import "fmt"
func main() {
m := make(map[string]string)
m["abc"] = ""
fmt.Println(m["a"] == m["abc"]) //true
}
If by "nothing" you mean that the element is not in the map you can use the ok idiom:
val, ok := myMap["value"] // ok is true if value was in the map
You can find more information in Effective Go.