go array

go array 特性

1、数组是值类型,赋值和传参会复制整个数组,⽽不是指针。
func printArray(x [2]int) {
	fmt.Printf("the array x address is:%p\n", &x)
	x[1] = 1000
}

func test1()  {
	var a [2]int = [2]int {1}
	fmt.Printf("the array a address is:%p\n", &a)
	fmt.Printf("the value of a:%v\n", a)
	printArray(a)
}

输出:

the array a address is:0xc0000a2070
the value of a:[1 0]
the array x address is:0xc0000a20a0

值拷⻉⾏为会造成性能问题,通常会建议使⽤ slice,或数组指针。

2、数组⻓度必须是常量,且是类型的组成部分。[2]int 和 [3]int 是不同类型。

如果类型相同,数组⽀持 “==”、"!=" 操作符,用来判断数组的值是否相同。

func test2() {
	a := [2]int {1,2}
	b := [2]int {1,2}

	// error:non-constant array bound length
	// var length int = 3
	// c := [length]int {}
	c := [3]int {}

	fmt.Printf("the type of a: %T, the value: %v\n", a, a)
	fmt.Printf("the type of b: %T, the value: %v\n", b, b)
	fmt.Printf("the type of c: %T, the value: %v\n", c, c)

	if a == b {
		fmt.Printf("a == b\n")
		fmt.Printf("the address of a: %p\n", &a)
		fmt.Printf("the address of b: %p\n", &b)
	}

	// error: invalid operation: a != c (mismatched types [2]int and [3]int)
	// if a != c {
	// 	fmt.Printf("a != c\n")
	// 	fmt.Printf("the address of a: %p\n", &a)
	// 	fmt.Printf("the address of c: %p\n", &c)
	// }

}

输出:

he type of a: [2]int, the value: [1 2]
the type of b: [2]int, the value: [1 2]
the type of c: [3]int, the value: [0 0 0]
a == b
the address of a: 0xc000012090
the address of b: 0xc0000120a0

3、指针数组 [n]*T,数组指针 *[n]T。

4、数组的初始化
func test3() {
	a := [3]int {1,2}				// 未初始化元素值为 0。
	b := [...]int {1,2,3,4}			// 通过初始化值确定数组⻓度。
	c := [5]int {2:10, 4:100}		// 使⽤索引号初始化元素。
	d := [...]int {3:100, 5:100}	// 可以利用索引确定数组长度
    
	e := [...]struct {
		name string
		age int
	} {
		{"chen",25},				// 可省略元素类型。
		{"yang",25},				// 别忘了最后⼀⾏的逗号。
	}

	fmt.Printf("the type of a: %T, the value: %v\n", a, a)
	fmt.Printf("the type of b: %T, the value: %v\n", b, b)
	fmt.Printf("the type of c: %T, the value: %v\n", c, c)
	fmt.Printf("the type of d: %T, the value: %v\n", d, d)
	fmt.Printf("the type of e: %T, the value: %v\n", e, e)

}

输出:

the type of a: [3]int, the value: [1 2 0]
the type of b: [4]int, the value: [1 2 3 4]
the type of c: [5]int, the value: [0 0 10 0 100]
the type of d: [6]int, the value: [0 0 0 100 0 100]
the type of e: [2]struct { name string; age int }, the value: [{chen 25} {yang 25}]

5、多维数组,先确定列的数量,再确定行的数量。
func test4() {
	a := [2][3]int {{1,1,1}, {2,2,2}}
	b := [...][2]int {		// 第 2 纬度不能⽤ "..."。
		{1,1},
		{2,2},
		{3,3},
	}

	fmt.Printf("the type of a: %T, the value: %v\n", a, a)
	fmt.Printf("the type of b: %T, the value: %v\n", b, b)
}

输出:

the type of a: [2][3]int, the value: [[1 1 1] [2 2 2]]
the type of b: [3][2]int, the value: [[1 1] [2 2] [3 3]]

6、数组长度。
func test5() {
	a := [2]int {}
	fmt.Println("the len of a: ", len(a)," the cap of a: ", cap(a))
}

输出:

the len of a:  2  the cap of a:  2

内置函数 len 和 cap 都能返回数组⻓度 (元素数量)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Golang 中,你可以使用数组来存储多个 `time.Time` 类型的值。下面是一个示例代码: ```go package main import ( "fmt" "time" ) func main() { // 创建一个长度为 3 的 time.Time 类型的数组 times := [3]time.Time{} // 初始化数组元素 times[0] = time.Now() times[1] = times[0].Add(time.Hour) times[2] = times[1].Add(time.Hour) // 遍历数组并输出每个元素的值 for _, t := range times { fmt.Println(t) } } ``` 输出结果类似于: ``` 2022-05-16 19:38:27.242514 +0800 CST m=+0.000162443 2022-05-16 20:38:27.242514 +0800 CST m=+3600.000162443 2022-05-16 21:38:27.242514 +0800 CST m=+7200.000162443 ``` 在这个示例中,我们创建了一个长度为 3 的 `time.Time` 类型的数组,并将其初始化为当前时间和当前时间加上一小时和两小时的值。然后,我们遍历数组并输出每个元素的值。 ### 回答2: 在Golang中,可以使用array来存储time.Time类型的数据。time.Time是Golang中用于表示日期和时间的结构体,它包含年、月、日、时、分、秒等信息。 首先,我们可以定义一个包含多个time.Time元素的数组。例如,我们可以定义一个长度为5的数组,存储5个不同的时间: ```go var times [5]time.Time times[0] = time.Now() times[1] = time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC) times[2] = time.Date(2022, time.February, 14, 12, 0, 0, 0, time.UTC) times[3] = time.Date(2022, time.March, 8, 9, 30, 0, 0, time.UTC) times[4] = time.Date(2022, time.April, 30, 18, 15, 0, 0, time.UTC) ``` 在上面的例子中,我们使用time.Now()获取当前时间,并使用time.Date()创建了一些特定的时间。这些时间被分别存储在了数组的不同索引位置。 我们还可以通过索引来访问数组中的时间元素,并使用各种时间相关的函数进行操作。例如,我们可以计算两个时间之间的差值: ```go diff := times[1].Sub(times[0]) fmt.Println(diff) // 输出: 8760h0m0s,即一年的小时数 ``` 还可以判断一个时间是否在另一个时间之前或之后: ```go isAfter := times[3].After(times[2]) // 判断times[3]是否在times[2]之后 isBefore := times[4].Before(times[3]) // 判断times[4]是否在times[3]之前 ``` 以上只是使用数组存储time.Time类型数据的一些简单示例。在实际应用中,我们可以根据需要使用数组来存储和操作多个时间。使用Golang的数组和time包,能够便捷地处理时间相关的问题。 ### 回答3: golang中的array是一种固定长度且类型相同的数据结构,而time.Time是golang中用于表示时间的类型。 在golang中,我们可以使用array来创建一个存储time.Time类型的数组。例如,我们可以使用以下代码创建一个包含3个time.Time类型元素的数组: ```go package main import ( "fmt" "time" ) func main() { var dates [3]time.Time dates[0] = time.Now() dates[1] = time.Now().Add(24 * time.Hour) dates[2] = time.Now().Add(48 * time.Hour) fmt.Println(dates) } ``` 在上面的例子中,我们定义了一个长度为3的time.Time类型的数组dates。我们可以使用数组索引来访问每个元素,并对其进行赋值。在这个例子中,我们分别将当前时间、明天的时间和后天的时间分别赋值给了数组的第1个、第2个和第3个元素。最后,我们通过fmt.Println来打印整个数组。 运行上述代码,你会得到一个包含3个时间的数组的输出结果。 总结一下,golang中的array可以用来存储time.Time类型的元素,通过索引来访问和赋值。这样我们就可以方便地使用数组来处理时间相关的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值