Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
dfff1829
Commit
dfff1829
authored
Apr 16, 2009
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update tutorial for new slicing rules.
R=rsc DELTA=13 (6 added, 0 deleted, 7 changed) OCL=27539 CL=27541
parent
b340879c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
7 deletions
+13
-7
go_tutorial.txt
doc/go_tutorial.txt
+13
-7
No files found.
doc/go_tutorial.txt
View file @
dfff1829
...
...
@@ -188,8 +188,12 @@ In Go, since arrays are values, it's meaningful (and useful) to talk
about pointers to arrays.
The size of the array is part of its type; however, one can declare
a <i>slice</i> variable, to which one can assign any array value
with the same element type. Slices look a lot like arrays but have
a <i>slice</i> variable, to which one can assign a pointer to
any array
with the same element type or - much more commonly - a <i>slice
expression</i> of the form "a[low : high]", representing
the subarray indexed by "low" through "high-1".
Slices look a lot like arrays but have
no explicit size ("[]" vs. "[10]") and they reference a segment of
an underlying, often anonymous, regular array. Multiple slices
can share data if they represent pieces of the same array;
...
...
@@ -203,7 +207,8 @@ of an array stored within your structure, you should use a regular
array.
When passing an array to a function, you almost always want
to declare the formal parameter to be a slice. Go will automatically
to declare the formal parameter to be a slice. When you call
the function, take the address of the array and Go will automatically
create (efficiently) a slice reference and pass that.
Using slices one can write this function (from "sum.go"):
...
...
@@ -217,16 +222,17 @@ and invoke it like this:
Note how the return type ("int") is defined for "sum()" by stating it
after the parameter list.
The expression "[3]int{1,2,3}" -- a type followed by a brace-bounded expression
-- is a constructor for a value, in this case an array of 3 "ints". We pass it
to "sum()" by (automatically) promoting it to a slice.
-- is a constructor for a value, in this case an array of 3 "ints". Putting an "&"
in front gives us the address of a unique instance of the value. We pass the
pointer to "sum()" by (automatically) promoting it to a slice.
If you are creating a regular array but want the compiler to count the
elements for you, use "..." as the array size:
s := sum([...]int{1,2,3});
s := sum(
&
[...]int{1,2,3});
In practice, though, unless you're meticulous about storage layout within a
data structure, a slice
- using empty brackets
- is all you need:
data structure, a slice
itself - using empty brackets and no "&"
- is all you need:
s := sum([]int{1,2,3});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment