mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Update array (Go).
This commit is contained in:
		@ -8,11 +8,6 @@ import (
 | 
			
		||||
	"math/rand"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
我们将 Go 中的 Slice 切片看作 Array 数组,降低理解成本,
 | 
			
		||||
有利于我们将关注点放在数据结构与算法上。
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
/* 随机返回一个数组元素 */
 | 
			
		||||
func randomAccess(nums []int) (randomNum int) {
 | 
			
		||||
	// 在区间 [0, nums.length) 中随机抽取一个数字
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,11 @@ import (
 | 
			
		||||
/* Driver Code */
 | 
			
		||||
func TestArray(t *testing.T) {
 | 
			
		||||
	/* 初始化数组 */
 | 
			
		||||
	var arr []int
 | 
			
		||||
	var arr [5]int
 | 
			
		||||
	fmt.Println("数组 arr =", arr)
 | 
			
		||||
	// 在 Go 中,指定长度时([5]int)为数组,不指定长度时([]int)为切片
 | 
			
		||||
	// 由于 Go 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
 | 
			
		||||
	// 为了方便实现扩容 extend() 方法,以下将切片(Slice)看作数组(Array)
 | 
			
		||||
	nums := []int{1, 3, 2, 5, 4}
 | 
			
		||||
	fmt.Println("数组 nums =", nums)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -44,7 +44,10 @@ comments: true
 | 
			
		||||
 | 
			
		||||
    ```go title="array.go"
 | 
			
		||||
    /* 初始化数组 */
 | 
			
		||||
    var arr []int
 | 
			
		||||
    var arr [5]int
 | 
			
		||||
    // 在 Go 中,指定长度时([5]int)为数组,不指定长度时([]int)为切片
 | 
			
		||||
    // 由于 Go 的数组被设计为在编译期确定长度,因此只能使用常量来指定长度
 | 
			
		||||
    // 为了方便实现扩容 extend() 方法,以下将切片(Slice)看作数组(Array)
 | 
			
		||||
    nums := []int{1, 3, 2, 5, 4}
 | 
			
		||||
    ```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user