Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ksakae1216
Created July 2, 2018 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksakae1216/a6f23ce501f560d94d49649526f32c3b to your computer and use it in GitHub Desktop.
Save ksakae1216/a6f23ce501f560d94d49649526f32c3b to your computer and use it in GitHub Desktop.
package funcpkg
import (
"fmt"
"strconv"
)
// SliceFunc スライスのサンプルです
func SliceFunc() {
val := []string{"A", "B", "C", "D"}
fmt.Println("変更前  -> ", val)
// スライスで関数に渡す(参照型なので値を変更できる)
changeVal(val[:])
fmt.Println("変更後  -> ", val)
// スライスに要素を追加
val = append(val[:], "E", "F")
fmt.Println("追加後  -> ", val)
// スライス要素のコピー
copy(val[:], []string{"A", "B"})
fmt.Println("コピー後 -> ", val)
// スライス要素に途中からコピー
copy(val[2:], []string{"C", "D"})
fmt.Println("コピー後 -> ", val)
}
func changeVal(valList []string) {
for index := 0; index < len(valList); index++ {
// スライスの値を変更
valList[index] = valList[index] + strconv.Itoa(index+1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment