Created
July 5, 2015 05:32
-
-
Save pocke/ec062ef22b25b075916d to your computer and use it in GitHub Desktop.
golang reslice sample
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 main | |
import ( | |
"reflect" | |
"testing" | |
) | |
func TestReSlice(t *testing.T) { | |
check := func(l, r []int) { | |
if !reflect.DeepEqual(l, r) { | |
t.Errorf("%q != %q", l, r) | |
} | |
} | |
// idx is 0, 1, 2, 3, 4 | |
s := []int{1, 2, 3, 4, 5} | |
// おなじ | |
check(s, s[0:len(s)]) | |
// 省略可能 | |
check(s, s[:len(s)]) | |
check(s, s[0:]) | |
check(s, s[:]) | |
// 始点のindexと、終点のindex+1を渡す | |
// s[n:m] は、Rubyで言ったら s[n...m] みたいな感じだと理解している。 | |
// (Rubyの ... は、終点を含まない Range のインスタンス) | |
check([]int{1, 2, 3, 4}, s[:len(s)-1]) | |
check([]int{2, 3, 4}, s[1:len(s)-1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment