Go 언어를 처음 사용합니다. 나는 자신이 array
과 slice
데이터 타입과 혼동하는 것을 발견했다.lang 배열 및 슬라이스 데이터 유형 이동
There are major differences between the ways arrays work in Go and C. In Go,
- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int and [20]int are distinct.
기능 : 나는 그것을 선언 할 때
As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to.
왜 sort.Ints(arrayValue)
가 전달 된 변수를 수정 않습니다를 배열로가 아닌 슬라이스로 다음과 같이
, 배열은 설명?
코드
var av = []int{1,5,2,3,7}
fmt.Println(av)
sort.Ints(av)
fmt.Println(av)
return
출력
[1 5 2 3 7]
[1 2 3 5 7]
또한 sort.Ints를 사용하면 슬라이스가 아닌 배열. sort.Ints 문서는 슬라이스를 지정합니다. https://golang.org/pkg/sort/#Ints – Kirk
@Kirk 좋은 지적. 나는 그것을 더 많은 가시성을 위해 대답에 포함 시켰습니다. – VonC