Skip to content

Instantly share code, notes, and snippets.

@broccoli1002
Last active June 23, 2018 06:40
Show Gist options
  • Save broccoli1002/6c6ad01b8937fdaa39fec7d4248af1ef to your computer and use it in GitHub Desktop.
Save broccoli1002/6c6ad01b8937fdaa39fec7d4248af1ef to your computer and use it in GitHub Desktop.
スターティングGO言語: スライス拡張 p175
package main
import (
"fmt"
)
func main() {
/* A */
a := [3]int{1, 2, 3} // s == [1,2,3]
s := a[:]
fmt.Println(len(s)) // == 3
fmt.Println(cap(s)) // == 3
fmt.Println(a) // [1,2,3]
fmt.Println(s) // [1,2,3]
/* B */
s[0] = 8
fmt.Println(a) // [8,2,3]
fmt.Println(s) // [8,2,3]
a[0] = 10
fmt.Println(a) // [10,2,3]
fmt.Println(s) // [10,2,3]
/* C */
// 容量が拡張されるとsはaの領域ではなくなる
s = append(s, 4) // a == [10,2,3], s == [10,2,3,4]
fmt.Println(a) // [10,2,3]
fmt.Println(s) // [10,2,3,4]
fmt.Println(len(s)) // == 4
fmt.Println(cap(s)) // == 8
a[0] = 9
fmt.Println(a) // [9,2,3]
fmt.Println(s) // [1,2,3,4]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment