golang: passing pointer to struct to function doesn't update that struct's field in called function [duplicate] - go

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

Related

extract values from map interface [duplicate]

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"])
}

Method prints correct data but returns unedited data in Go [duplicate]

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!

How can I insert a new element to slice when the method is self defined? [duplicate]

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?

golang: Why the following code does NOT panic with "slice index out of range" error [duplicate]

This question already has answers here:
Appending one element to nil slice increases capacity by two
(4 answers)
Closed 6 years ago.
package main
import "fmt"
type Point struct {
X int
Y int
}
type Points struct {
P []Point
}
func main() {
data := Points{}
for i := 0; i < 10; i++ {
data.P = append(data.P, Point{
X: i,
Y: i*2,
})
}
fmt.Printf("%+v\n", data.P[5:11]);
}
While the above program is run, it printed out:
[{X:5 Y:10} {X:6 Y:12} {X:7 Y:14} {X:8 Y:16} {X:9 Y:18} {X:0 Y:0}]
Why there are {X:0, Y:0} which seems to be automatically generated as the slice's length is 10 but I try to get 5:11?
I found the problem in my code and test with "raw" slice such as:
package main
import "fmt"
func main() {
v := []int{0,1,2,3,4,5,6,7,8,9}
fmt.Printf("%v\n", v[5:11])
}
This simple program generates errors (as expected):
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
panic(0x47a8e0, 0xc42000a130)
/usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
/home/fxr/go/src/cmhwc/test2.go:7 +0x53
exit status 2
Why the first program does not panic?
Because when you use append() to add elements to a slice which has no more room for adding the new elements it creates a new one doubling its capacity copying the old elements as well as the new you're asking to add and returning a reference to that. Further down when you use slice syntax to ask for elements past the apparent end of the slice the length is actually applied to the underlying array created & returned by append.
As #JimB suggested in the comments, you can add a statement to print the capacity at every step in the append loop to see this happening:
fmt.Println(cap(data.P))
Should generate something like:
2
2
4
4
8
8
8
8
16
16

How to differentiate empty string and nothing in a map [duplicate]

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.

Resources