Created
July 2, 2018 11:58
-
-
Save ksakae1216/a6f23ce501f560d94d49649526f32c3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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