소개
Slices 랩은 Go 의 slice 데이터 타입에 대한 지식을 테스트하도록 설계되었습니다. Slices 는 배열보다 더 강력한 시퀀스 인터페이스이며, 이 랩은 slices 를 사용하는 방법을 이해하는 데 도움이 될 것입니다.
Slices (슬라이스)
이 랩에서 해결해야 할 문제는 Go 에서 슬라이스를 생성하고 조작하는 것입니다. 길이가 0 이 아닌 빈 슬라이스를 생성하고, 슬라이스에서 값을 설정하고 가져오고, len 함수를 사용하여 슬라이스의 길이를 얻고, append 함수를 사용하여 슬라이스에 새 값을 추가하고, copy 함수를 사용하여 슬라이스를 복사하고, 슬라이스 연산자를 사용하여 기존 슬라이스에서 요소의 슬라이스를 가져와야 합니다.
이 랩을 완료하려면 Go 구문과 슬라이스 데이터 타입에 대한 기본적인 이해가 필요합니다. 또한 make, append, copy 함수와 슬라이스 연산자에 익숙해야 합니다.
## Note that while slices are different types than arrays,
## they are rendered similarly by `fmt.Println`.
$ go run slices.go
emp: [ ]
set: [a b c]
get: c
len: 3
apd: [a b c d e f]
cpy: [a b c d e f]
sl1: [c d e]
sl2: [a b c d e]
sl3: [c d e f]
dcl: [g h i]
2d: [[0] [1 2] [2 3 4]]
## Check out this [great blog post](https://go.dev/blog/slices-intro)
## by the Go team for more details on the design and
## implementation of slices in Go.
## Now that we've seen arrays and slices we'll look at
## Go's other key builtin data structure: maps.
전체 코드는 다음과 같습니다.
// _Slices_ are an important data type in Go, giving
// a more powerful interface to sequences than arrays.
package main
import "fmt"
func main() {
// Unlike arrays, slices are typed only by the
// elements they contain (not the number of elements).
// To create an empty slice with non-zero length, use
// the builtin `make`. Here we make a slice of
// `string`s of length `3` (initially zero-valued).
s := make([]string, 3)
fmt.Println("emp:", s)
// We can set and get just like with arrays.
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])
// `len` returns the length of the slice as expected.
fmt.Println("len:", len(s))
// In addition to these basic operations, slices
// support several more that make them richer than
// arrays. One is the builtin `append`, which
// returns a slice containing one or more new values.
// Note that we need to accept a return value from
// `append` as we may get a new slice value.
s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)
// Slices can also be `copy`'d. Here we create an
// empty slice `c` of the same length as `s` and copy
// into `c` from `s`.
c := make([]string, len(s))
copy(c, s)
fmt.Println("cpy:", c)
// Slices support a "slice" operator with the syntax
// `slice[low:high]`. For example, this gets a slice
// of the elements `s[2]`, `s[3]`, and `s[4]`.
l := s[2:5]
fmt.Println("sl1:", l)
// This slices up to (but excluding) `s[5]`.
l = s[:5]
fmt.Println("sl2:", l)
// And this slices up from (and including) `s[2]`.
l = s[2:]
fmt.Println("sl3:", l)
// We can declare and initialize a variable for slice
// in a single line as well.
t := []string{"g", "h", "i"}
fmt.Println("dcl:", t)
// Slices can be composed into multi-dimensional data
// structures. The length of the inner slices can
// vary, unlike with multi-dimensional arrays.
twoD := make([][]int, 3)
for i := 0; i < 3; i++ {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := 0; j < innerLen; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}
요약
Slices 랩은 Go 의 slice 데이터 타입에 대한 지식을 테스트하도록 설계되었습니다. 이 랩을 완료함으로써, 슬라이스에서 값을 설정하고 가져오는 방법, len 함수 사용법, append 및 copy 함수 사용법, 그리고 슬라이스 연산자 사용법을 포함하여 Go 에서 슬라이스를 생성하고 조작하는 방법에 대한 더 나은 이해를 얻게 될 것입니다.