다시 a tour of go 봐야 할 듯..
Arrays The type [n]T is an array of n values of type T. The expression var a [10]int declares a variable a as an array of ten integers. An array's length is part of its type, so arrays cannot be resized. This seems limiting, but don't worry; Go provides a convenient way of working with arrays. |
[링크 : https://go.dev/tour/moretypes/6]
Slices An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays. The type []T is a slice with elements of type T. A slice is formed by specifying two indices, a low and high bound, separated by a colon: a[low : high] This selects a half-open range which includes the first element, but excludes the last one. The following expression creates a slice which includes elements 1 through 3 of a: a[1:4] |
[링크 : https://go.dev/tour/moretypes/7]
크기가 정해지면 array, 정해지지않으면 slice인가?
Since you didn't specify the length, it is a slice. An array type definition specifies a length and an element type: see "Go Slices: usage and internals" |
[링크 : https://stackoverflow.com/questions/29361377/creating-a-go-slice-without-make]
갑자기 length와 capacity?!
A slice literal is declared just like an array literal, except you leave out the element count: letters := []string{"a", "b", "c", "d"} A slice can be created with the built-in function called make, which has the signature, func make([]T, len, cap) []T where T stands for the element type of the slice to be created. The make function takes a type, a length, and an optional capacity. When called, make allocates an array and returns a slice that refers to that array. var s []byte s = make([]byte, 5, 5) // s == []byte{0, 0, 0, 0, 0} When the capacity argument is omitted, it defaults to the specified length. Here’s a more succinct version of the same code: s := make([]byte, 5) The length and capacity of a slice can be inspected using the built-in len and cap functions. len(s) == 5 cap(s) == 5 |
[링크 : https://go.dev/blog/slices-intro]
실제 사용시에 capacity까지 알 필요는 없을려나?
capacity: 실제 메모리에 할당된 공간입니다. 만약 슬라이스에 요소를 추가하여 capacity가 가득차면 자동으로 늘어납니다. |
[링크 : https://www.pymoon.com/entry/Go-튜토리얼-배열-슬라이스]
[링크 : https://phsun102.tistory.com/82]
[링크 : https://go.dev/tour/moretypes/11]
2차원 배열은 [][]type 으로 생성하면되는데
make를 통해서도 2차원 배열이 생성가능한진 모르겠다
package main import "fmt" func main() { // Step 1: create empty collection. values := [][]int{} // Step 2: these are the first two rows. // ... Append each row to the two-dimensional slice. row1 := []int{1, 2, 3} row2 := []int{4, 5, 6} values = append(values, row1) values = append(values, row2) // Step 3: display first row, and second row. fmt.Println("Row 1") fmt.Println(values[0]) fmt.Println("Row 2") fmt.Println(values[1]) // Step 4: access an element. fmt.Println("First element") fmt.Println(values[0][0]) } |
Row 1 [1 2 3] Row 2 [4 5 6] First element 1 |
[링크 : https://www.dotnetperls.com/2d-go]
make와 := []type 두개가 동등하다면 가능할지도?
package main import ( "fmt" "strings" ) func main() { // Create a tic-tac-toe board. board := [][]string{ []string{"_", "_", "_"}, []string{"_", "_", "_"}, []string{"_", "_", "_"}, } // The players take turns. board[0][0] = "X" board[0][2] = "X" board[1][0] = "O" board[1][2] = "X" board[2][2] = "O" for i := 0; i < len(board); i++ { fmt.Printf("%s\n", strings.Join(board[i], " ")) } } |
X _ X O _ X _ _ O |
'Programming > golang' 카테고리의 다른 글
golang 타입 땜시 짜증 (0) | 2023.11.10 |
---|---|
golang 타입 캐스팅 제약(?) (0) | 2023.11.09 |
golang ini 지원 (0) | 2023.11.07 |
golang 함수인자에 배열 포인터 (0) | 2023.11.07 |
c to golang online converter (0) | 2023.11.07 |