Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use arrays in Go language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章主要介绍Go语言中数组怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

概述

固定长度,数组声明后长度便不能再修改

只能存储一种特定类型元素的序列

语法编号方式代码示例1直接声明var arr [3]int2makearr:=make([]int,3)3字面量arr:=[3]int{1,2,3}4自动识别长度arr:=[…]int{1,2,3}5二维数组arr := [4][4]int{{1}, {1, 2}, {1, 2, 3}}6newarrp := new([10]int)7下标取值arr[0-size-1]注意

1. 使用new来创建数组,此方法返回一个指向数组的指针

2. 数组之间可以使用==或!=进行比较,但不可以使用

3. 数组在Go中为值类型

4.注意区分指向数组的指针和指针数组

示例package mainimport "fmt"func main() { //直接声明 var ages [3]int fmt.Println(ages) //字面量 names := [3]string{"张三", "李四", "王五"} fmt.Println(names) //make arr := make([]int, 3) fmt.Println(arr) //自动识别长度 arr1 := [...]string{"张三", "李四"} fmt.Println(arr1) //new arr2 := new([3]int) fmt.Println(arr2) //二维数组 iarray6 := [4][4]int32{{1}, {1, 2}, {1, 2, 3}} fmt.Println(iarray6) //数组截取【下标是从0开始】 fmt.Println(iarray6[1:2]) //指针 p := new([4]int) fmt.Println(*p)}以上是"Go语言中数组怎么用"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report