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

Re-organize "Array, strings và slices" section #12

Closed
cuonglm opened this issue Sep 10, 2021 · 1 comment
Closed

Re-organize "Array, strings và slices" section #12

cuonglm opened this issue Sep 10, 2021 · 1 comment

Comments

@cuonglm
Copy link

cuonglm commented Sep 10, 2021

Describe the bug

The "Array, strings và slices" section has some wrong concepts/definitions, and also somewhat not clearly, since when it introduces strings before slices. The official Go blog has an introduction post about "Arrays, slices (and strings)".

To Reproduce

At revision e19da2e:

  1. Wrong definition of uintptr
type StringHeader struct {
    // con trỏ địa chỉ vùng nhớ string
    Data uintptr
    // chiều dài của string
    Len  int
}

This is wrong, Data is an uintptr (Numeric types), not a pointer type, even though its value is the pointer address in memory. This is important concept, otherwise, it can lead to wrong assumption and cause memory corruption. The official Go documentation has a warning for this.

The same problem applies to the slices section:

Data: là con trỏ chứa địa chỉ của một array.
  1. Wrong description about len builtin

Tương tự như array, String cũng có một hàm built-in là len dùng để trả về chiều dài của string

len is not a function of String, it's a builtin function, which can take various types of argument, including string type.

  1. Wrong word about ranging over slice

Duyệt qua slice thì tương tự như duyệt qua một arrays.

This is not true. Ranging over an array requires copying the array elements, while ranging over slice is not (the same as ranging over pointer to array).

  1. The description of slice growing capacity is not quite right

Trong trường hợp slice ban đầu không đủ sức chứa khi thêm vào phần tử, hàm append sẽ hiện thực cấp phát lại vùng nhớ có kích thước gấp đôi vùng nhớ cũ và sao chép dữ liệu sang.

This is only right for slice with < 1024 element, for slice with >= 1024 element, the grow factor is only 1.25 (and the factor can be smoother in go1.18, see https://go-review.googlesource.com/c/go/+/347917)

Expected behavior

Re-organizing the section to follow official Go blog, and also correct all the wrong definition.

@phamtai97
Copy link
Collaborator

phamtai97 commented Sep 11, 2021

hi @cuonglm,
Thanks for your feedback. I have reviewed and corrected the issues you mentioned. I created a PR here to fix it.
In the future, we very happy if you can contribute to this project to make this project better for the community.

@cuonglm cuonglm closed this as completed Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants