我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

切片的最后一个元素

What is the Go way for extracting the last element of a slice?

var slice []int

slice = append(slice, 2)
slice = append(slice, 7)

slice[len(slice)-1:][0] // Retrieves the last element

The solution above works, but seems awkward.

转载于: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

Bit less elegant but can also do:

sl[len(sl)-1: len(sl)]

近期文章

  • 去安装总是失败 GOPATH 以外没有安装目录
  • 如何修剪一个字符串的领先和拖尾空白?
  • 如何真正!彻底!解决antimalware占内存问题
  • 什么是最好的 Scheme 解释器或编译器? [打烊]
  • 什么是' (点或句点)在 Go 导入语句做什么?
  • 函数声明语法: 函数名前的括号内的内容
  • 为什么要编码为数据?
  • 外汇基金资产负债表摘要及
  • 指针可以指向内存,是不是没有分配?
  • sscanf() skipping delimiter & width
  • 用于 c,openmp 的 CAS 实现
  • 戈朗随机数发生器如何正确播种
  • 避免与 pthreads 发生内存冲突
  • 避免文件中出现空白
  • C Unicode: 我如何应用 C11标准修正案 DR488修正 C11标准功能 c16rtomb () ?
  • 在戈朗中迭代时更改值
  • 如果语句在 c 语言中不能正确工作
  • @ 符号在 PHP 中有什么用?
  • 链接到 c 语言中的静态库
  • 程序中的额外输出

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华