DEV Community

Discussion on: ทำไม Go ต้องมี pointer และเราใช้ pointer ใน Go ตอนไหนบ้าง

Collapse
 
miwtoo profile image
Miwtoo
func doubleAllElement(nums []int) {
    for i := range nums {
        nums[i] *= 2
    }
}

func main() {
    nums := []int{1, 2, 3}
    doubleAllElement(nums)
    fmt.Println(nums)
}

การส่ง arrays ก็เป็นการส่งค่าของ address ไปด้วยหรอครับ แล้วตอนมีการเปลี่ยนค่า ก็ไม่ต้องใช้ * เหมือน struct ใช่ไหมครับ

Collapse
 
iporsut profile image
Weerasak Chongnguluam

slice นะครับ ไม่ใช่ array ตัว slice เองเก็บ 3 อย่าง Len, cap, และ address ของ element

Collapse
 
miwtoo profile image
Miwtoo

ขอบคุณมากครับ