Help us understand the problem. What is going on with this article?

【Go】スライスのサンプルメモ

package main

import (
"fmt"
"reflect"
)

func main() {
// 配列
arr := [3]string{"a", "b", "c"}

// スライス型の変数を宣言
var s1 []string
// スライス
s1 = arr[:]
    fmt.Println(s1) // [a b c]

    fmt.Println(reflect.TypeOf(s1)) // []string

// var を使わないVersion
s2 := arr[:]
fmt.Println(s2) // [a b c]

// インデックスを指定
s3 := arr[1:2]
fmt.Println(s3) // [b c]

s4 := arr[1:3]
fmt.Println(s4) // [b c]

// append
s5 := append(s4, "d", "e")
fmt.Println(s4) // [b c]
fmt.Println(s5) // [b c d e]

// 配列とスライスを同時に宣言
s6 := []string{"A", "B", "C"}
fmt.Println(s6)

// 長さ、キャパシティ
fmt.Println(len(s6)) // 3
fmt.Println(cap(s6)) // 3

s7 := append(s6, "D")
fmt.Println(len(s7)) // 4
fmt.Println(cap(s7)) // 6 --> 倍になった

a8 := [...]int{0, 1, 2, 3, 4}
s8 := a8[1:4]
fmt.Println(s8) // [1 2 3]
fmt.Println(len(s8)) // 3
fmt.Println(cap(s8)) // 4

s9 := a8[1:2]
fmt.Println(len(s9)) // 1
fmt.Println(cap(s9)) // 4

s10 := a8[0:5]
fmt.Println(len(s10)) // 5
fmt.Println(cap(s10)) // 5

}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした