当前位置:网站首页>Go array and slice, []byte to string[easy to understand]
Go array and slice, []byte to string[easy to understand]
2022-06-28 13:45:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Array : A fixed length sequence of the same data type Definition of array :var a [len]int , such as var a[5]int Length is part of the array type , therefore ,var a [5]int and var a [10]int It's a different type
func Array() {
// Initialization of an array
var age0 [5]int = [5]int{1,2,3}
var age1 = [5]int{1,2,3,4,5}
var age2 = [...]int{1,2,3,4,5,6}
var str = [5]string{3:"hello world",4:"john"} // Specify the third and fourth elements of the array
fmt.Println(age0)
fmt.Println(age1)
fmt.Println(age2)
fmt.Println(str)
}
func DemiArray() {
// Multidimensional arrays Define a two-dimensional array with three rows and four columns
var a [3][4]int = [...][4]int{
{1,2,3,4},{5,6,7,8},{9,10,11,12}}
for _,v:= range a{
for _,v1 := range v{
fmt.Printf("%d\t",v1)
}
fmt.Printf("\n")
}
}
func main() {
Array()
DemiArray()
}section : A slice is a reference to an array , So slices are of reference type The length of the slice is variable , So the slice is a mutable array Slices are traversed in the same way as arrays , It can be used len() Find the length cap We can work out slice Maximum capacity of ,0 <= len(slice) <= cap(array), among array yes slice Array of references Definition of slice :var Variable name [] type , such as var str []string var arr []int
func silce() {
// Define an array and cut slices from the array
var a = [10]int{1,2,3,4}
b := a[1:5]
fmt.Println(b)
// Defining slices , And use make Function initialization
var s = []int
s = make([]int,5)
s[0] = 1
fmt.Println(s)
// One slice plus another slice
var c = []int{1,2,3}
var d = []int{4,5,6,7}
c = append(c,d...) // section append section
fmt.Println(c)
// A copy of the slice , Built in functions copy
s1 :=[]int{1,2,3,4,5}
s2 := make([]int,10)
copy(s2,s1)
fmt.Printf("s2:%d\n",s2)
}[]byte Interturn string s1 := “hello,word” s2 :=[]byte(s1) //string turn []byte s1 = string(s2) //[]byte turn string
func modifystring () {
//slice And string The relationship between
/*string It is immutable in itself , So change string String in , The following operations are required */
str := "hello world"
s := []byte(str)
s[0] = '0'
str = string(s)
fmt.Println(str)
// But if the string is in Chinese, it can be converted to byte The array of type is wrong , Should use the
s1 := " I love you! , China "
s2 := []rune(s1)
s2[0] = ' you '
s1 = string(s2)
fmt.Println(s1)
}use slice Non recursive realization of Fibonacci sequence
func fibo(n int){
// use slice Non recursive implementation Fibonacci The sequence
var a [] int64
a = make([]int64,n)
a[0] = 1
a[1] = 1
for i := 2; i < n; i++{
a[i] = a[i-1] +a[i-2]
}
for _,v := range a{
fmt.Println(v,)
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150608.html Link to the original text :https://javaforall.cn
边栏推荐
- 决策树预测红酒品质
- 股票网上开户及开户流程怎样?手机开户是安全么?
- 再谈exception——异常抛出时会发生什么?
- Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
- MySQL multi table joint query
- En parlant d'exception - que se passe - t - il lorsque l'exception est lancée?
- Websocket automatically disconnects in 1 minute
- Forecast and Analysis on market scale and development trend of China's operation and maintenance security products in 2022
- Unit test ci/cd
- PCB懂王,你是吗?我不是
猜你喜欢

众昂矿业着眼氟化工产业,布局新能源产业链

抢做意大利岛主?刘强东两月套现66亿 疑一次性5.6亿“紧急转账”急购欧洲海上皇宫

Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event

Kubernetes 深入理解Kubernetes(二) 声明组织对象

Special test for cold and hot start of app

Introduction to PWN (1) binary Basics

中国广电5G套餐来了,比三大运营商低,却没预期那么低

Kubernetes in-depth understanding of kubernetes (I)

程序员坐牢了,会被安排去写代码吗?

药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
随机推荐
MySQL multi table joint query
GPS数据格式的分析与处理[通俗易懂]
Double buffer drawing
What is generics and how to use generics analysis
RK3399平台开发系列讲解(使用篇)Pinctrl子系统的介绍 - 视频介绍
《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」
PostgreSQL超越MySQL
G : 最大流问题
Vscode shortcut key
初识exception
Jupyter notebook中添加虚拟环境
如何备份mysql_史上最全的MYSQL备份方法
N皇后问题
为什么越来越多的用户放弃 Swagger,选择Apifox
Rk3399 platform development series explanation (use part) pinctrl subsystem introduction - Video Introduction
程序员坐牢了,会被安排去写代码吗?
Pytorch Foundation
排序
Latest summary! 30 provinces announce 2022 college entrance examination scores
Pytorch main modules