"A language that doesn't affect the way you think about programming is not worth knowing."

Alan J. Perlis (1922-1990), first recipient of the Turing Award


Parameters in Go are always passed by value, and a copy of the value being passed is made. If you pass a pointer, then the pointer value will be copied and passed. When a slice is passed, the slice value (which is a small descriptor) will be copied and passed - which will point to the same backing array (which will not be copied).

-Stackoverflow


Go - Slice

Go'da slicelar bir fonksiyona parametre olarak verildiklerinde, orjinal value'yu referans ediyor. Bu sebeple fonksiyon icerisinde slice'in bir elemanini degistirdigimizde orjinal slice da degisir.

package main

import "fmt"

func main() {
    a := []int{1, 2, 3, 4}
    foo(a)

    fmt.Println(a) // [10, 2, 3, 4]
}

func foo(s []int) {
    s[0] = 10
}

Play

Yukarida goruldugu gibi foo fonksiyonuna slice'i pointer olarak vermememe ragmen orjinal slice degisti.

Go Slices: usage and internals


Hobaaa


func m() (i int){
    defer func(){i++}
    return 1
}

m() // 2

The more I learn, the more I'm convinced that there's nothing that can't be learned.

Jamis Buck