Go中如何提取切片的最后一个元素?
var slice []int
slice = append(slice, 2)
slice = append(slice, 7)
slice[len(slice)-1:][0] // Retrieves the last element
上面提供的这个解决方案是有效的,但是很麻烦。
转载于:https://stackoverflow.com/questions/22535775/the-last-element-of-a-slice
Go中如何提取切片的最后一个元素?
var slice []int
slice = append(slice, 2)
slice = append(slice, 7)
slice[len(slice)-1:][0] // Retrieves the last element
上面提供的这个解决方案是有效的,但是很麻烦。
转载于:https://stackoverflow.com/questions/22535775/the-last-element-of-a-slice
For just reading the last element of a slice:
sl[len(sl)-1]
For removing it:
sl = sl[:len(sl)-1]
See this page about slice tricks