当前位置:网站首页>Go language slicing operation
Go language slicing operation
2022-04-23 12:56:00 【Play ha ha 527】
package main
import (
"fmt"
)
func main() {
// Create slices , Using slice
slice:=make([]string,5)// length 5, The default capacity is also 5
//slice1:=make([]string,5,3)// length 5 Greater than capacity 3, error
slice2:=make([]int,3,5)// Create integer slice , length 3 Capacity 5
slice3:=[]string{
"red","blue","yellow"}// initialization
slice4:=[]int{
3,4,5,6}// Initialize integer slice
slice5:=[]string{
19:"test"}// Use string test Initializing page 20 Elements
// If in [] The operator specifies a value , So you're creating arrays instead of slices . Only if no value is specified
// When , To create a slice ,
array:=[3]int{
1,2,3}// Array
slice6:=[]int{
4,5,6}// section
// establish nil section
var slice7 []int
// Create an empty slice
slice8:=make([]int,0)
slice9:=[]int{
}
// Create an integer slice
// Its length and capacity are 5 Elements
slice_a := []int{
10, 20, 30, 40, 50}
slice_b:=slice_a[1:3]
fmt.Print(slice_a)
fmt.Println(slice_b)
slice_b[1]=35
newslice:=append(slice_a,60)
fmt.Print(slice)
//fmt.Print(slice1)
fmt.Print(slice2)
fmt.Print(slice3)
fmt.Println(slice4)
fmt.Println(slice5)
fmt.Println(array)
fmt.Println(slice6)
fmt.Print(slice7)
fmt.Print(slice8)
fmt.Println(slice9)
fmt.Print(slice_a)
fmt.Print(slice_b)
fmt.Print(newslice)
// Create two slices , Two integers are used for initialization
s1 := []int{
1, 2}
s2 := []int{
3, 4}
fmt.Printf("%v\n",append(s1,s2...))// Use ... Operator , You can append all the elements of one slice to another
Create an integer slice Its length and capacity are 4 Elements
slice_c := []int{
10, 20, 30, 40}
// Iterate over each element , And display its value
for index,value:=range slice_c {
fmt.Printf("index: %d,value: %d\n",index,value)
}
Create an integer slice , Multi slice
mul_slice := [][]int{
{
10}, {
100, 200}}
fmt.Println(mul_slice)
mul_slice[0]=append(mul_slice[0],30)
fmt.Println(mul_slice)
Allocation contains 100 Slice of 10000 integer values
slice10:=make([]int,10)
take slice10 Pass to function foo
slice10=foo(slice10)
fmt.Println(slice10)
}
func foo(slice []int) []int {
return slice
}
/* printout [10 20 30 40 50][20 30] [ ][0 0 0][red blue yellow][3 4 5 6] [ test] [1 2 3] [4 5 6] [][][] [10 20 35 40 50][20 35][10 20 35 40 50 60][1 2 3 4] index: 0,value: 10 index: 1,value: 20 index: 2,value: 30 index: 3,value: 40 [[10] [100 200]] [[10 30] [100 200]] [0 0 0 0 0 0 0 0 0 0] */
版权声明
本文为[Play ha ha 527]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615231417.html
边栏推荐
- Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
- 数据库中的日期时间类型
- Try the server for one month for free, and attach the tutorial
- Keyword interpretation and some APIs in RT thread
- mysql中 innoDB执行过程分析
- Resolve disagrees about version of symbol device_ create
- Fashion cloud learning - input attribute summary
- Date time type in database
- 98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
- STM32控制步进电机(ULN2003+28byj)
猜你喜欢
How to click an object to play an animation
box-sizing
31. Next arrangement
进程虚拟地址空间区域划分
Redis deployment of cloud native kubesphere
CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
解决disagrees about version of symbol device_create
XinChaCha Trust SSL Organization Validated
世界读书日:我想推荐这几本书
SSM framework series - data source configuration day2-1
随机推荐
Recommended website for drawing result map
数据库中的日期时间类型
Plato farm - a game of farm metauniverse with Plato as the goal
SSM框架系列——Junit单元测试优化day2-3
After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
Unable to create servlet under SRC subfile of idea
Common problems of unity (1)
有趣的IDEA插件推荐,给你的开发工作增添色彩
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
php生成json处理中文
The continuous construction of the Internet industry platform is not only able to collect traffic
Jiachen chapter Genesis "inner universe" joint Edition
RT-thread中关键词解释及部分API
SSM框架系列——数据源配置day2-1
Servlet监听器&过滤器介绍
如何防止网站被黑客入侵篡改
Calculate the past date and days online, and calculate the number of live days
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
[vulnhub range] - DC2