-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add golang code for the array #178
Conversation
本次提交包含如下示例代码。 - 遍历数组; - 初始化数组; - 扩展数组长度; - 在数组中查找指定元素; - 随机返回一个数组元素; - 删除索引 index 处元素; - 在数组的索引 index 处插入元素 num。 所有数组约定长度为 5。原因如下: 在 goalng 中,必须声明数组的长度,例如:nums := [5]int{1,2,3,4,5}。如果不声明长度,则被称为切片。 使用的注释没有按照 golang 的编程惯例,而是倾向于使用文档上下文的注释约定。 所以所有函数注释均使用了 `/* ... */`,而不是双斜杠 `//`。
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the professional code! 👍
However, this book aims to introduce data structure and algorithms. Thus it is recommended to make the test code simpler, ignoring part of professionalism. Could you refer to other Go codes and make the test code like them?
Moreover, please check the correspondence between the code with the reference code and make them as similar as possible. It will decrease some annoying problems caused by the mismatches.
"The length is part of the array's type and must be a constant expression that evaluates to a non-negative integer value." enlarge argument is variable, not a constant expression. https://go.dev/ref/spec#Array_types So I have to use constants instead of passing variable parameters in the function signature. If I use the passed parameters, this will be a slice, not an array. @krahets 😄 |
Thank you for your review. 😄 |
Yes. Let's make the code simpler. We want the readers to focus on the data structure and algorithm. |
Thanks for the explanation, let's use a slice. And add a comment "我们将 Go 中的 slice 看作是数组"。 |
- 去除了并行测试; - 基于 Java 代码样例,统一了命名风格; - 基于 Go link 模块代码样例,统一了测试用例风格; - 我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以降低理解成本,利于我们将关注点放在数据结构与算法上。
@krahets hi, I finished |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @guowei-gong , there are several comments to be addressed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There still a comment to be addressed
https://github.com/krahets/hello-algo/pull/178/files#r1058474851
Yes, it is a must. If I now have an array of length 5, then the max index should be 4, without it, index 5 will be accessed, which will generate an error.panic: runtime error: index out of range [5] with length 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank @guowei-gong for the hard work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine-tuned.
It is recommended to directly write as /* 删除索引 index 处元素 */
func remove(nums []int, index int) {
// 把索引 index 之后的所有元素向前移动一位
for i := index; i < len(nums) - 1; i++ {
nums[i] = nums[i+1]
}
} Because we did not set 0 as the |
本次提交包含如下示例代码。
✅ 已通过测试用例
所有数组约定长度为 5。原因如下:
在 goalng 中,必须声明数组的长度,例如:nums := [5]int{1,2,3,4,5}。如果不声明长度,则被称为切片。
使用的注释没有按照 golang 的编程惯例,而是倾向于使用文档上下文的注释约定。
所以所有函数注释均使用了
/* ... */
,而不是双斜杠//
。If this PR is related to coding or code translation, please fill out the checklist.