Here is my code and below that the output - I wrote my question into the output. But to summarize: How can a function modify one of its parameters ie a structure (with slices)? as seen below.
package main
import (
“fmt”
)
type bType struct {
s int // That should read s open square bracket - close square bracket - sorry see supplemental question at bottom
w int
}
func main() {
bOrig := bType{s: []int{11, 12, 13, 14, 15, 16}, w: []int{21, 22, 23, 24, 25, 26}}
bSecond := bOrig
bThird := bType{}
bThird = bOrig
bFourth := bType{}
bFourth = clonebType(bOrig)
fmt.Printf("After Creation\n bOrig: %v\n", bOrig)
fmt.Printf(" bSecond: %v\n", bSecond)
fmt.Printf(" bThird: %v\n", bThird)
fmt.Printf(" bFourth: %v\n", bFourth)
bSecond.s[2] += 70
bThird.s[4] += 80
fmt.Printf("\nAfter bSecond.s[2] += 70 and bThird.s[4] += 80\n bOrig: %v\n", bOrig)
fmt.Printf(" bSecond: %v\n", bSecond)
fmt.Printf(" bThird: %v\n", bThird)
fmt.Printf(" bFourth: %v\n", bFourth)
fmt.Printf("\nSIDE QUESTION:\n OK so I get it, in a structure with slices when copied with a \n simple = or := the slices still point to the same memory locations. \n Is that going to be true for primitive types or sub structures of non primitive types?")
fmt.Printf("\n\nPRIMARY QUESTION:\n Below I pass a structure (bOrig) containing slices to a \n method that changes part of the slice it then returns nothing.\n Upon return the structure that was passed reflects the changes made to it in the method.\n My question is: given this behavior how can Go be called a language that uses\n CALL BY VALUE???\n\n" +
"Am I missing something?")
bOrig.methodmessUpbType()
fmt.Printf("\nAfter bOrig.methodmessUpbType()\n bOrig: %v\n", bOrig)
fmt.Printf(" bSecond: %v\n", bSecond)
fmt.Printf(" bThird: %v\n", bThird)
fmt.Printf(" bFourth: %v\n", bFourth)
}
func clonebType(bIn bType) bType {
var bOut bType
bOut.s = make(int, len(bIn.s))
copy(bOut.s, bIn.s)
bOut.w = make(int, len(bIn.w))
copy(bOut.w, bIn.w)
return bOut
}
// Could not figure out how to install and use go-clone so wrote my own method: clonebType
//Be sure to INSTALL go get GitHub - huandu/go-clone: Clone any Go data structure deeply and thoroughly.
//“github.com/huandu/go-clone/generic”)
func (bIn bType) methodmessUpbType() {
bIn.s[0] += 1000
return
}
And the output:
GOROOT=C:\Users\dan\Dropbox\00 DropBox DAP\Go Language\go1.23.1 #gosetup
GOPATH=C:\Users\dan\go #gosetup
“C:\Users\dan\Dropbox\00 DropBox DAP\Go Language\go1.23.1\bin\go.exe” build -o C:\Users\dan\AppData\Local\JetBrains\GoLand2024.2\tmp\GoLand___go_build_testProject.exe . #gosetup
C:\Users\dan\AppData\Local\JetBrains\GoLand2024.2\tmp\GoLand___go_build_testProject.exe #gosetup
After Creation
bOrig: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
bSecond: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
bThird: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
bFourth: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
After bSecond.s[2] += 70 and bThird.s[4] += 80
bOrig: {[11 12 83 14 95 16] [21 22 23 24 25 26]}
bSecond: {[11 12 83 14 95 16] [21 22 23 24 25 26]}
bThird: {[11 12 83 14 95 16] [21 22 23 24 25 26]}
bFourth: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
SIDE QUESTION:
OK so I get it, in a structure with slices when copied with a
simple = or := the slices still point to the same memory locations.
Is that going to be true for primitive types or sub structures of non primitive types?
PRIMARY QUESTION:
** Below I pass a structure (bOrig) containing slices to a **
** method that changes part of the slice it then returns nothing.**
** Upon return the structure that was passed reflects the changes made to it in the method.**
** My question is: given this behavior how can Go be called a language that uses**
** CALL BY VALUE???**
Am I missing something?
After bOrig.methodmessUpbType()
bOrig: {[1011 12 83 14 95 16] [21 22 23 24 25 26]}
bSecond: {[1011 12 83 14 95 16] [21 22 23 24 25 26]}
bThird: {[1011 12 83 14 95 16] [21 22 23 24 25 26]}
bFourth: {[11 12 13 14 15 16] [21 22 23 24 25 26]}
Process finished with the exit code 0
My conclusion is that structures and slices are actually pointers? Or is this too simple an explanation? But still Call by Value?
Supplemental question - I pasted the code and output but as you can see it broke the listing up. Where should I read to learn how to do this correctly???