diff options
| author | Divay Prakash <divayprakash@users.noreply.github.com> | 2020-02-09 22:18:18 +0530 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-09 22:18:18 +0530 | 
| commit | e24cad5eef30ecba1e5a0acd7c559041bf34608f (patch) | |
| tree | b63e189eef61130b6acd5b4c585ddc5f182da094 /go.html.markdown | |
| parent | ccb727ebd36bf080d07a9e536a11fac61243d90c (diff) | |
| parent | cd631729a89faaf1cd77d7659e86c00be18e0309 (diff) | |
Merge pull request #3849 from petrroll/patch-2
[go/en] Add value/reference semantics information for arr/slices.
Diffstat (limited to 'go.html.markdown')
| -rw-r--r-- | go.html.markdown | 10 | 
1 files changed, 10 insertions, 0 deletions
| diff --git a/go.html.markdown b/go.html.markdown index ec812f20..739ec05d 100644 --- a/go.html.markdown +++ b/go.html.markdown @@ -109,6 +109,11 @@ can include line breaks.` // Same string type.  	a5 := [...]int{3, 1, 5, 10, 100} // An array initialized with a fixed size of five  	// elements, with values 3, 1, 5, 10, and 100. +	// Arrays have value semantics. +	a4_cpy := a4            // a4_cpy is a copy of a4, two separate instances. +	a4_cpy[0] = 25          // Only a4_cpy is changed, a4 stays the same. +	fmt.Println(a4_cpy[0] == a4[0]) // false +  	// Slices have dynamic size. Arrays and slices each have advantages  	// but use cases for slices are much more common.  	s3 := []int{4, 5, 9}    // Compare to a5. No ellipsis here. @@ -116,6 +121,11 @@ can include line breaks.` // Same string type.  	var d2 [][]float64      // Declaration only, nothing allocated here.  	bs := []byte("a slice") // Type conversion syntax. +	// Slices (as well as maps and channels) have reference semantics. +	s3_cpy := s3            // Both variables point to the same instance. +	s3_cpy[0] = 0           // Which means both are updated. +	fmt.Println(s3_cpy[0] == s3[0]) // true	 +  	// Because they are dynamic, slices can be appended to on-demand.  	// To append elements to a slice, the built-in append() function is used.  	// First argument is a slice to which we are appending. Commonly, | 
