go tourã§ããããªãã£ããã¨ã®ã¡ã¢ã§ã
package main import "fmt" func main() { s := []int{2, 3, 5, 7, 11, 13} s = s[1:4] fmt.Println(s) s = s[:2] fmt.Println(s) s = s[1:] fmt.Println(s) }
çµæã¯
[3 5 7] [3 5] [5]
ã§ãã ããã»ã»ã»ãªãã»ã©åãåã£ã¦ããã¦ä¾¿å©ã ããã¨æã£ã¦ãã¾ãã
試ãã«æ¹å¤ãã¦ã¿ããæ³å®å¤ã®çµæã«ãªãã¾ãã
package main import "fmt" func main() { s := []int{1,2, 3, 4,5,6,7,8,9,10} s = s[0:8] fmt.Println(s) s = s[:2] fmt.Println(s) s = s[:4] fmt.Println(s) }
Result
[1 2 3 4 5 6 7 8] [1 2] [1 2 3 4]
æå¾ã¯ã¨ã©ã¼ã«ãªãããªã»ã»ã»ã¨æã£ã¦ãããæ³å®å¤ã®çµæãè¿ã£ã¦ããã®ã§èª¿ã¹ã¦ã¿ã
- æåã®
s = s[0:8]
ã§ãå ã®s
ã®ã¤ã³ããã¯ã¹0ãã7ã¾ã§ã®è¦ç´{1, 2, 3, 4, 5, 6, 7, 8}
ãæã¤æ°ããã¹ã©ã¤ã¹ãçæãããs
ãããã«ç½®ãæããã¾ãã - 次ã«
s = s[:2]
ã§ãs
ã¯ã¤ã³ããã¯ã¹0ãã1ã¾ã§ã®è¦ç´{1, 2}
ãæã¤ã¹ã©ã¤ã¹ã«ãªãã¾ãã - æå¾ã«
s = s[:4]
ãå®è¡ããã¨ãs
ã¯ç¾å¨ã®ã¹ã©ã¤ã¹{1, 2}
ã®å é ãã4ã¤ã®è¦ç´ ãå«ãã¹ã©ã¤ã¹ã«åå®ç¾©ããã¾ãããã ããå ã®ã¹ã©ã¤ã¹ã®è¦ç´ æ°ãè¶ ãã¦ã¤ã³ããã¯ã¹4ã¾ã§ã®è¦ç´ ãåããã¨ãã¦ãããããGoè¨èªã§ã¯å ã®ã¹ã©ã¤ã¹ã®é·ãã«å¿ãã¦èªåçã«èª¿æ´ããã¾ãã
ã¾ããã»ã»ã»ã¨ã©ã¼ã«ãªã£ãæ¹ã楽ãªãã ãã©