The other answers to your original question are reasonable, and probably helpful. But I would also like to point out the definitive, though maybe less helpful answer to both your original question, and this new one. You see that behavior because the language specification says you should. Read https://golang.org/ref/spec#Slice_expressions, particualrly the "Simple slice expressions" section.
On Thursday, March 7, 2019 at 9:12:28 PM UTC-5, Halbert.Collier Liu wrote: > > Yes, i see, > thank you so much! > > Could you please explain, why primes[6:6] okay, but primes[7:7] not? > *:-)* > > > 在 2019年3月7日星期四 UTC+8下午11:55:40,Robert Johnstone写道: >> >> Hello, >> >> When you use the colon, you taking a subset of the data. Further, the >> notation is a closed/open. So a slice primes[6:6] is all of the element in >> the array with index >= 6 and index < 6, which is an empty set. Note that >> the type of the expression primes[6:6] is []int. >> >> When you don't use the colon, you are access a specific element. Since >> the count is zero based, the valid indices are 0 through 5 inclusive. Note >> that the type of the expression primes[6] is simply int. >> >> Good luck. >> >> >> On Thursday, 7 March 2019 10:32:04 UTC-5, Halbert.Collier Liu wrote: >>> >>> Hi. >>> >>> The code like below: >>> >>> package main >>> >>> import "fmt" >>> >>> func main() { >>> primes := [6]int{2, 3, 5, 7, 11, 13} >>> fmt.Println(primes[6:6]) . // *OK*. return: [] >>> //fmt.Println(primes[6]) . // fail. out of bounds... >>> } >>> >>> Why? >>> >>> Is the golang grammatical feature? or anything else.. >>> >>> Any help, please! >>> >> -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.