- 배열은 값 복사
- 슬라이스는 참조형
- 슬라이스는 배열의 표현 형식
package main
import (
"fmt"
)
func main() {
var arr = [3]int{0, 0, 0} // 배열에 리터럴 값 지정
fmt.Printf("arr's type: %T\n", arr)
var sls = []int{0, 0, 0} // 슬라이스에 리터럴 값 지정
fmt.Printf("sls's type: %T\n", sls)
fmt.Println("Before func's arr:", arr)
receiveArray(arr) // 함수에서 받은 배열의 요소를 바꿨다.
fmt.Println("After func's arr:", arr) // 원본 배열은 바뀌지 않는다.
fmt.Println("Before func's sls:", sls)
receiveSlice(sls)
fmt.Println("After func's sls:", sls) // 함수를 다녀오면 원본 슬라이스도 바뀐다.
}
func receiveArray(arr [3]int) {
arr[0] = 1
arr[1] = 1
arr[2] = 1
}
func receiveSlice(sls []int) {
sls[0] = 1
sls[1] = 1
sls[2] = 1
}
// 결과
//
// arr's type: [3]int
// sls's type: []int
// Before func's arr: [0 0 0]
// After func's arr: [0 0 0]
// Before func's sls: [0 0 0]
// After func's sls: [1 1 1]
package main
import (
"fmt"
)
func main() {
grades := [...]int{1, 2, 3}
fmt.Printf("Grades: %v\n", grades)
var person [3]string
person[0] = "Mike"
person[1] = "Lisa"
person[2] = "John"
fmt.Printf("Person: %v\n", person)
fmt.Printf("Person #3: %v\n", person[2])
fmt.Printf("Num of Person: %v\n", len(person))
people := person // copy entire array to new array (not reference)
people[2] = "sammy"
fmt.Printf("Person: %v\n", person)
fmt.Printf("People: %v\n", people)
fmt.Println("==========")
PEOPLE := &person // copy address of array to variable
PEOPLE[2] = "sammy"
fmt.Printf("Person: %v\n", person)
fmt.Printf("PEOPLE: %v\n", PEOPLE)
// slcie
arr := [...]int{1, 2, 3}
fmt.Printf("arr's type: %T\n", arr)
sls := arr[:]
fmt.Printf("sls's type: %T\n", sls)
fmt.Println("sls original:", sls)
sls[0] = 9 // slice is reference type.
fmt.Println("sls changed:", sls)
fmt.Println("arr was effected:", arr)
// slices don't have to have a fixed size over there entire life.
newSls := make([]int, 3, 100)
fmt.Println(newSls)
fmt.Println("Len:", len(newSls))
fmt.Println("Cap:", cap(newSls))
fmt.Println("==========")
x := []int{1, 2, 3, 4, 5}
fmt.Println("x:", x)
y := append(x[:2], x[3:]...) // be aware of this behavior!!!
fmt.Println("y:", y)
fmt.Println("x:", x)
}
// 결과
//
// Grades: [1 2 3]
// Person: [Mike Lisa John]
// Person #3: John
// Num of Person: 3
// Person: [Mike Lisa John]
// People: [Mike Lisa sammy]
// ==========
// Person: [Mike Lisa sammy]
// PEOPLE: &[Mike Lisa sammy]
// arr's type: [3]int
// sls's type: []int
// sls original: [1 2 3]
// sls changed: [9 2 3]
// arr was effected: [9 2 3]
// [0 0 0]
// Len: 3
// Cap: 100
// ==========
// x: [1 2 3 4 5]
// y: [1 2 4 5]
// x: [1 2 4 5 5]
package main
import (
"fmt"
)
func main() {
sls := []int{1, 2, 3, 4, 5}
// create new slice and append part of sls to it
var new_sls []int
new_sls = append(sls, 9)
fmt.Println("new_sls:", new_sls)
// append new value to itself
sls = append(sls, 0)
fmt.Println("sls:", sls)
// append a slice to an existing slice
slice1 := []int{1, 2, 3}
slice2 := []int{4, 5, 6}
slice2 = append(slice2, slice1...) // ... ellipsis used to append a slice
fmt.Println("slice2:", slice2)
tar := []int{1, 2, 3}
var src []int
// src = append(src, tar[:1]) // error
src = append(src, tar[:1]...)
fmt.Println("src:", src)
}
// 결과
//
// new_sls: [1 2 3 4 5 9]
// sls: [1 2 3 4 5 0]
// slice2: [4 5 6 1 2 3]
// src: [1]
- 슬라이스의 삭제 (새로운 슬라이스로 제외해서 복사해서 리턴)
package main
import (
"fmt"
"sort"
)
func main() {
var animals []string
animals = append(animals, "dog")
animals = append(animals, "fish")
animals = append(animals, "cat")
animals = append(animals, "horse")
sort.Strings(animals)
fmt.Println("Is it sorted now?", sort.StringsAreSorted(animals))
fmt.Println(animals)
animals = deleteFromSlice(animals, 1)
fmt.Println(animals)
}
func deleteFromSlice(s []string, i int) []string {
s[i] = s[len(s)-1]
s[len(s)-1] = ""
s = s[:len(s)-1]
return s
}
// 결과
//
// Is it sorted now? true
// [cat dog fish horse]
// [cat horse fish]
댓글 영역