docs(array): sample code for golang
本次提交包含如下示例代码。 - 遍历数组; - 初始化数组; - 扩展数组长度; - 在数组中查找指定元素; - 随机返回一个数组元素; - 删除索引 index 处元素; - 在数组的索引 index 处插入元素 num。 所有数组约定长度为 5。原因如下: 在 goalng 中,必须声明数组的长度,例如:nums := [5]int{1,2,3,4,5}。如果不声明长度,则被称为切片。 使用的注释没有按照 golang 的编程惯例,而是倾向于使用文档上下文的注释约定。 所以所有函数注释均使用了 `/* ... */`,而不是双斜杠 `//`。
This commit is contained in:
parent
b01297660a
commit
ca8c1849f6
@ -135,7 +135,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Go"
|
||||
|
||||
```go title="array.go"
|
||||
|
||||
/* 随机返回一个数组元素 */
|
||||
func randomAccess(nums [5]int) (ans int) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
randomIndex := rand.Intn(len(nums))
|
||||
// 获取并返回随机元素
|
||||
ans = nums[randomIndex]
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -238,7 +245,20 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Go"
|
||||
|
||||
```go title="array.go"
|
||||
|
||||
// 在 Go 中,声明数组长度必须是常量表达式,
|
||||
// 所以这里我们约定,扩展后的长度为 6
|
||||
const expectSize = 6
|
||||
// 扩展数组长度
|
||||
func extend(nums [5]int) [expectSize]int {
|
||||
// 初始化一个扩展长度后的数组
|
||||
var res [expectSize]int
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
res[i] = nums[i]
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -372,7 +392,32 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Go"
|
||||
|
||||
```go title="array.go"
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
func insert(nums [5]int, num int, index int) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
// 如果超出了数组长度,会被直接舍弃
|
||||
for i := len(nums) - 1; i > index; i-- {
|
||||
nums[i] = nums[i-1]
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
func remove(nums [5]int, index int) {
|
||||
// 越界检查
|
||||
if index >= len(nums) {
|
||||
return
|
||||
}
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for i := index; i < len(nums); i++ {
|
||||
if i+1 >= len(nums) {
|
||||
nums[len(nums)-1] = 0
|
||||
break
|
||||
}
|
||||
nums[i] = nums[i+1]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -501,7 +546,18 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Go"
|
||||
|
||||
```go title="array.go"
|
||||
|
||||
/* 遍历数组 */
|
||||
func traverse(nums [5]int) {
|
||||
var count int
|
||||
// 通过索引遍历数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
}
|
||||
// 直接遍历数组
|
||||
for index, val := range nums {
|
||||
fmt.Printf("index:%d value:%d\n", index, val)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -606,7 +662,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Go"
|
||||
|
||||
```go title="array.go"
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
func find(nums [5]int, target int) (ans int){
|
||||
ans = -1
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == target {
|
||||
ans = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
Loading…
Reference in New Issue
Block a user