Right here is my code and under that the output – I wrote my query into the output. However to summarize: How can a perform modify certainly one of its parameters ie a construction (with slices)? as seen under.
bundle most important
import (
“fmt”
)
sort bType struct {
s int // That ought to learn s open sq. bracket – shut sq. bracket – sorry see supplemental query at backside
w int
}
func most important() {
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 Creationn bOrig: %vn", bOrig)
fmt.Printf(" bSecond: %vn", bSecond)
fmt.Printf(" bThird: %vn", bThird)
fmt.Printf(" bFourth: %vn", bFourth)
bSecond.s[2] += 70
bThird.s[4] += 80
fmt.Printf("nAfter bSecond.s[2] += 70 and bThird.s[4] += 80n bOrig: %vn", bOrig)
fmt.Printf(" bSecond: %vn", bSecond)
fmt.Printf(" bThird: %vn", bThird)
fmt.Printf(" bFourth: %vn", bFourth)
fmt.Printf("nSIDE QUESTION:n OK so I get it, in a construction with slices when copied with a n easy = or := the slices nonetheless level to the identical reminiscence areas. n Is that going to be true for primitive varieties or sub buildings of non primitive varieties?")
fmt.Printf("nnPRIMARY QUESTION:n Beneath I go a construction (bOrig) containing slices to a n technique that modifications a part of the slice it then returns nothing.n Upon return the construction that was handed displays the modifications made to it within the technique.n My query is: given this conduct how can Go be known as a language that usesn CALL BY VALUE???nn" +
"Am I lacking one thing?")
bOrig.methodmessUpbType()
fmt.Printf("nAfter bOrig.methodmessUpbType()n bOrig: %vn", bOrig)
fmt.Printf(" bSecond: %vn", bSecond)
fmt.Printf(" bThird: %vn", bThird)
fmt.Printf(" bFourth: %vn", 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
}
// Couldn’t determine how you can set up and use go-clone so wrote my very own technique: clonebType
//You should definitely INSTALL go get GitHub – huandu/go-clone: Clone any Go knowledge construction deeply and totally.
//“github.com/huandu/go-clone/generic”)
func (bIn bType) methodmessUpbType() {
bIn.s[0] += 1000
return
}
And the output:
GOROOT=C:UsersdanDropbox 0 DropBox DAPGo Languagego1.23.1 #gosetup
GOPATH=C:Usersdango #gosetup
“C:UsersdanDropbox 0 DropBox DAPGo Languagego1.23.1bingo.exe” construct -o C:UsersdanAppDataLocalJetBrainsGoLand2024.2tmpGoLand___go_build_testProject.exe . #gosetup
C:UsersdanAppDataLocalJetBrainsGoLand2024.2tmpGoLand___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 construction with slices when copied with a
easy = or := the slices nonetheless level to the identical reminiscence areas.
Is that going to be true for primitive varieties or sub buildings of non primitive varieties?
PRIMARY QUESTION:
** Beneath I go a construction (bOrig) containing slices to a **
** technique that modifications a part of the slice it then returns nothing.**
** Upon return the construction that was handed displays the modifications made to it within the technique.**
** My query is: given this conduct how can Go be known as a language that makes use of**
** CALL BY VALUE???**
Am I lacking one thing?
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]}
Course of completed with the exit code 0
My conclusion is that buildings and slices are literally pointers? Or is that this too easy an evidence? However nonetheless Name by Worth?
Supplemental query – I pasted the code and output however as you may see it broke the itemizing up. The place ought to I learn to discover ways to do that accurately???