Skip to content
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

Merged
merged 5 commits into from
Dec 30, 2022
Merged

Add golang code for the array #178

merged 5 commits into from
Dec 30, 2022

Conversation

guowei-gong
Copy link
Contributor

本次提交包含如下示例代码。

  • 遍历数组;
  • 初始化数组;
  • 扩展数组长度;
  • 在数组中查找指定元素;
  • 随机返回一个数组元素;
  • 删除索引 index 处元素;
  • 在数组的索引 index 处插入元素 num。

✅ 已通过测试用例

所有数组约定长度为 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.

  • I've tested the code and ensured the outputs are the same as the outputs of reference codes.
  • I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
  • The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

本次提交包含如下示例代码。
- 遍历数组;
- 初始化数组;
- 扩展数组长度;
- 在数组中查找指定元素;
- 随机返回一个数组元素;
- 删除索引 index 处元素;
- 在数组的索引 index 处插入元素 num。

所有数组约定长度为 5。原因如下:
在 goalng 中,必须声明数组的长度,例如:nums := [5]int{1,2,3,4,5}。如果不声明长度,则被称为切片。

使用的注释没有按照 golang 的编程惯例,而是倾向于使用文档上下文的注释约定。
所以所有函数注释均使用了 `/* ... */`,而不是双斜杠 `//`。
@vercel
Copy link

vercel bot commented Dec 27, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Updated
hello-algo ⬜️ Ignored (Inspect) Dec 30, 2022 at 8:45AM (UTC)

@guowei-gong
Copy link
Contributor Author

@krahets

Copy link
Owner

@krahets krahets left a 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.

@guowei-gong
Copy link
Contributor Author

guowei-gong commented Dec 27, 2022

"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 😄

@guowei-gong
Copy link
Contributor Author

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.

Thank you for your review. 😄
I will try to modify it. I checked other people's code. The test case is a hybrid pattern. Are you sure this is the desired result? If you use the go programming style, the current pr is better. Maybe I can create another test case with mixed patterns, while keeping the files in the current pr. It's up to you, friend.

@krahets
Copy link
Owner

krahets commented Dec 27, 2022

The test case is a hybrid pattern. Are you sure this is the desired result?

Yes. Let's make the code simpler. We want the readers to focus on the data structure and algorithm.

@krahets
Copy link
Owner

krahets commented Dec 27, 2022

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.

Thanks for the explanation, let's use a slice. And add a comment "我们将 Go 中的 slice 看作是数组"。

- 去除了并行测试;
- 基于 Java 代码样例,统一了命名风格;
- 基于 Go link 模块代码样例,统一了测试用例风格;
- 我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以降低理解成本,利于我们将关注点放在数据结构与算法上。
@guowei-gong
Copy link
Contributor Author

@krahets hi, I finished

Copy link
Owner

@krahets krahets left a 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.

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guowei-gong
Copy link
Contributor Author

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

Copy link
Owner

@krahets krahets left a 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!

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine-tuned.

@krahets krahets merged commit 0950e43 into krahets:master Dec 30, 2022
@krahets
Copy link
Owner

krahets commented Dec 30, 2022

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

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 empty, we do not have to change the tail number as 0.
Moreover, if you change the code from the reference language (like here setting the tail number as 0), please describe the reason in the PR. We aim to keep the consistency of the codes of all the languages.

@krahets krahets added the code Code-related label Jan 2, 2023
@krahets krahets changed the title docs(array): sample code for golang Add golang code for the array Jan 2, 2023
@Reanon Reanon mentioned this pull request Jan 8, 2023
36 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants