当前位置:网站首页>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
边栏推荐
- thinkphp6 多级控制器目录访问解决方法
- Prediction of red wine quality by decision tree
- 你的代码会说话吗?(上)
- How fragrant! The most complete list of common shortcut keys for pychar!
- China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share
- Why will the new 5g standard bring lower TCO to the technology stack
- 2.01 backpack problem
- (原创)【MAUI】一步一步实现“悬浮操作按钮”(FAB,Floating Action Button)
- Multi dimensional monitoring: the data base of intelligent monitoring
- CVPR再起争议:IBM中稿论文被指照搬自己承办竞赛第二名的idea
猜你喜欢

China Database Technology Conference (DTCC) specially invited experts from Kelan sundb database to share

MySQL多表联合查询

Solution to directory access of thinkphp6 multi-level controller

MySQL multi table joint query

How to solve the problem that the computer wireless network does not display the network list

初识exception

China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected

Hubble数据库x某股份制商业银行:冠字号码管理系统升级,让每一张人民币都有 “身份证”

StackOverflow 2022数据库年度调查

Make an ink screen electronic clock, cool!
随机推荐
Elastic box auto wrap demo
公司领导说,个人代码超10个Bug就开除,是什么体验?
How to open an account of Huatai Securities? How to handle the account opening is the safest
再談exception——异常拋出時會發生什麼?
Luogu_ P1303 A*B Problem_ High precision calculation
如何设计数据可视化平台
StackOverflow 2022数据库年度调查
DevEco Studio 3.0编辑器配置技巧篇
Latest summary! 30 provinces announce 2022 college entrance examination scores
ThreadLocal的简单理解
Websocket automatically disconnects in 1 minute
thinkphp6 多级控制器目录访问解决方法
Connected to rainwater series problems
木兰开放作品许可证1.0面向社会公开征求意见
《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」
股票网上开户及开户流程怎样?手机开户是安全么?
Writing skills of resume
PCB understand Wang, are you? I am not
BERT为何无法彻底干掉BM25??
APP冷热启动专项测试