Go 语言教程:8.数组

在 Go 语言中,数组是具有固定大小、相同数据类型的元素集合。本教程将通过代码示例,逐步解释如何声明、初始化和操作数组。

Go 复制代码
package main

import "fmt"

func main() {

    var a [5]int
    fmt.Println("emp:", a)

    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])

    fmt.Println("len:", len(a))

    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)

    b = [...]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)

    b = [...]int{100, 3: 400, 500}
    fmt.Println("idx:", b)

    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)

    twoD = [2][3]int{
        {1, 2, 3},
        {1, 2, 3},
    }
    fmt.Println("2d: ", twoD)
}
相关推荐
chao1898444 小时前
基于 SPEA2 的多目标优化算法 MATLAB 实现
开发语言·算法·matlab
赏金术士4 小时前
Kotlin 习题集 · 高级篇
android·开发语言·kotlin
楼兰公子5 小时前
buildroot 在编译rust时裁剪平台类型数量的方法
开发语言·后端·rust
吴声子夜歌5 小时前
Go——并发编程
开发语言·后端·golang
ooseabiscuit6 小时前
Laravel4.x:现代PHP框架的奠基之作
java·开发语言·php
c1s2d3n4cs6 小时前
Qt模仿nlohmann::json进行序列化和反序列化
开发语言·qt·json
AiTop1007 小时前
Claude Code 推出 Agent View:命令行编程正式进入“多线程并发“时代
开发语言·人工智能·ai·aigc
jf加菲猫7 小时前
第21章 Qt WebEngine
开发语言·c++·qt·ui
码农-阿杰7 小时前
深入理解 synchronized 底层实现:从 HotSpot C++ 源码看对象锁与 Monitor 机制
开发语言·c++·
2401_832298108 小时前
AI智能体监管落地,OpenClaw率先建立行业合规标准
开发语言