Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
_Fighter
Learngo
提交
5fd0977d
L
Learngo
项目概览
_Fighter
/
Learngo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
11
Wiki
分析
仓库
DevOps
项目成员
Pages
L
Learngo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
11
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5fd0977d
编写于
12月 08, 2021
作者:
_Fighter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
2021年12月7日
上级
c2f0ebfa
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
63 addition
and
0 deletion
+63
-0
sliceops.go
sliceops.go
+45
-0
slices.go
slices.go
+18
-0
未找到文件。
sliceops.go
0 → 100644
浏览文件 @
5fd0977d
package
main
import
"fmt"
func
printSlice
(
s
[]
int
)
{
// cap 扩展是成倍增加
fmt
.
Printf
(
"%d , len=%d,cap=%d
\n
"
,
s
,
len
(
s
),
cap
(
s
))
}
func
main
()
{
var
s
[]
int
//定义一个slice ,未赋值 Zero value for slice is nil
for
i
:=
0
;
i
<
100
;
i
++
{
printSlice
(
s
)
s
=
append
(
s
,
2
*
i
+
1
)
}
fmt
.
Println
(
s
)
s1
:=
[]
int
{
2
,
3
,
4
,
5
,
6
,
5
,
7
}
printSlice
(
s1
)
sx
:=
append
(
s1
,
3
,
4
,
5
)
// 当前 s1 cap 长度为7 添加数据时长度*2 结果 cap=14
printSlice
(
sx
)
//指定 len 长度的slice
s2
:=
make
([]
int
,
16
)
// 指定 len ,cap
s3
:=
make
([]
int
,
10
,
32
)
printSlice
(
s2
)
printSlice
(
s3
)
fmt
.
Println
(
"Copying slice"
)
// 把s1 copy 到 s2
copy
(
s2
,
s1
)
printSlice
(
s2
)
// go 是没有内建的 删除函数 ,只能手动删除
fmt
.
Println
(
"deleting elements for slice"
)
// 当前 s2 的值为 [2 3 4 5 6 5 7 0 0 0 0 0 0 0 0 0] 删除 5,6 两个值
s2
=
append
(
s2
[
:
3
],
s2
[
5
:
]
...
)
//可变长参数加...
printSlice
(
s2
)
fmt
.
Println
(
"Popping from front"
)
// 删除头
s2
=
s2
[
1
:
]
// 删除尾
s2
=
s2
[
:
len
(
s2
)
-
1
]
printSlice
(
s2
)
}
slices.go
浏览文件 @
5fd0977d
...
@@ -57,4 +57,22 @@ func main() {
...
@@ -57,4 +57,22 @@ func main() {
s2=[5 6], len(s2)=2 ,cap(s2)=3
s2=[5 6], len(s2)=2 ,cap(s2)=3
*/
*/
//添加元素时如果超越cap,系统会重新分配更大的层数组
// 由于值会传递关系,必须接收append的返回值
fmt
.
Println
(
" slice 相关操作方法"
)
op
:=
[]
int
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
9
}
fmt
.
Println
(
"op :"
,
op
)
t1
:=
op
[
2
:
6
]
fmt
.
Println
(
"t1:"
,
t1
)
t2
:=
append
(
t1
,
10
,
10
)
fmt
.
Println
(
"t2:"
,
t2
)
fmt
.
Println
(
"op :"
,
op
)
// 覆盖原数组数据,在 cap 以内的
//
}
func
showLenCap
(
s
[]
int
)
{
fmt
.
Printf
(
"value:%s ,len(s)%d,cap(s)%d"
,
s
,
len
(
s
),
cap
(
s
))
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录