Browse files

Merge remote-tracking branch 'wayneeseguin/code-readability'

Conflicts:
	interfaces.rst
  • Loading branch information...
2 parents 67486cb + 7af517d commit 76061d70b141d492cbadcb99e0b9ec90d1b2c6b0 @initpy committed Jan 3, 2012
Showing with 402 additions and 386 deletions.
  1. +37 −37 composite.rst
  2. +55 −49 control.rst
  3. +24 −24 even-more-functions.rst
  4. +39 −40 functions.rst
  5. +2 −3 interfaces.rst
  6. +34 −34 maps.rst
  7. +71 −69 more-functions.rst
  8. +140 −130 slices.rst
View
74 composite.rst
@@ -63,9 +63,9 @@ How to declare a struct in Go?
.. code-block:: go
:linenos:
- type person struct{
- name string //field name of type string.
- age int //field age of type int.
+ type person struct {
+ name string // Field name of type string.
+ age int // Field age of type int.
}
See? It's easy. A person ``struct`` is a container of two fields:
@@ -81,12 +81,12 @@ Thus, writing things like:
.. code-block:: go
:linenos:
- type person struct{
+ type person struct {
name string
age int
}
- var P person // tom is a variable of type person
+ var P person // Tom will be a variable of type person. How quaint!
We access and assign a struct's fields (attributes) with the *dot nottation*.
That is, if ``P`` is a variable of type ``person``, then we access its fields like
@@ -95,9 +95,9 @@ this:
.. code-block:: go
:linenos:
- P.name = "Tom" //assign "Tom" to P's name field.
- P.age = 25 //set P's age to 25 (an int)
- fmt.Println("The person's name is %s", P.name) //Access P's name field.
+ P.name = "Tom" // Assign "Tom" to P's name field.
+ P.age = 25 // Set P's age to 25 (an int).
+ fmt.Println("The person's name is %s", P.name) // Access P's name field.
There's even two short-form assignment statement syntaxes:
@@ -131,27 +131,26 @@ Good. Let's write our ``Older`` function that takes two input parameters of type
import "fmt"
// We declare our new type
- type person struct{
+ type person struct {
name string
age int
}
-
- //return the older person of p1 and p2
- //and the difference in their ages
- func Older(p1, p2 person) (person, int){
- if p1.age>p2.age { //compare p1 and p2's ages
+ // Return the older person of p1 and p2
+ // and the difference in their ages.
+ func Older(p1, p2 person) (person, int) {
+ if p1.age>p2.age { // Compare p1 and p2's ages
return p1, p1.age-p2.age
}
return p2, p2.age-p1.age
}
- func main(){
+ func main() {
var tom person
tom.name, tom.age = "Tom", 18
- //Look how to declare and initialize easily
+ // Look how to declare and initialize easily.
bob := person{age:25, name:"Bob"} //specify the fields and their values
paul := person{"Paul", 43} //specify values of fields in their order
@@ -239,12 +238,13 @@ For example, the name of the 3 :sup:`rd` person is:
.. code-block:: go
:linenos:
- //declare an array A of 10 persons
+ // Declare an array A of 10 persons:
var people [10]person
- //Remember indices start from 0!
+ // Remember indices start from 0!
third_name := people[2].name //use the dot to access the third 'name'.
- //or more verbosely:
+
+ // Or more verbosely:
third_person := people[2]
thirdname := third_person.name
@@ -276,36 +276,36 @@ The idea now is to write a function ``Older10`` that takes an array of 10
import "fmt"
//Our struct
- type person struct{
+ type person struct {
name string
age int
}
//return the older person in a group of 10 persons
- func Older10(people [10]person) person{
- older := people[0] //the first one is the older for now
- //loop through the array and check if we could find an older person.
- for i:= 1; i<10; i++{ //we skipped the first here
- if people[i].age > older.age{ //compare the current's person age with the older one
- older = people[i] //if people[i] is older, replace the value of older
+ func Older10(people [10]person) person {
+ older := people[0] // The first one is the older for now.
+ // Loop through the array and check if we could find an older person.
+ for index := 1; index < 10; index++ { // We skipped the first element here.
+ if people[index].age > older.age { // Current's persons age vs olderest so far.
+ older = people[index] // If people[index] is older, replace the value of older.
}
}
return older
}
- func main(){
- // declare an example array variable of 10 person called: array
+ func main() {
+ // Declare an example array variable of 10 person called 'array'.
var array [10]person
- // initialize some of the elements of the array, the others ar by default
- // set to person{"", 0}
+ // Initialize some of the elements of the array, the others ar by
+ // default set to person{"", 0}
array[1] = person{"Paul", 23};
array[2] = person{"Jim", 24};
array[3] = person{"Sam", 84};
array[4] = person{"Rob", 54};
array[8] = person{"Karl", 19};
- older := Older10(array) //call the function by passing it our array
+ older := Older10(array) // Call the function by passing it our array.
fmt.Println("The older of the group is: ", older.name)
}
@@ -323,7 +323,7 @@ function above in a single shot like this:
.. code-block:: go
:linenos:
- //declare and initialize an array A of 10 person
+ // Declare and initialize an array A of 10 person.
array := [10]person {
person{"", 0},
person{"Paul", 23},
@@ -345,8 +345,8 @@ given in the initialization for us. So we could have written the above code as:
.. code-block:: go
:linenos:
- //declare and initialize an array of 10 person, but let the compiler guess the size.
- array := [...]person { // substitute '...' for the size of the array.
+ // Declare and initialize an array of 10 persons, but let the compiler guess the size.
+ array := [...]person { // Substitute '...' instead of an integer size.
person{"", 0},
person{"Paul", 23},
person{"Jim", 24},
@@ -393,7 +393,7 @@ We can declare a 2-dimensional array like this:
:linenos:
//declare and initialize an array of 2 arrays of 4 ints
- a := [2][4]int {[4]int{1,2,3,4}, [4]int{5,6,7,8}}
+ double_array := [2][4]int {[4]int{1,2,3,4}, [4]int{5,6,7,8}}
This is an array of (2 arrays (of 4 ``int``)). We can think of it as a matrix,
or a table of two lines each made of 4 columns.
@@ -424,7 +424,7 @@ count arrays' elements for us, like this:
:linenos:
//simplify the previous declaration, with the '...' syntax
- a := [2][4]int {[...]int{1,2,3,4}, [...]int{5,6,7,8}}
+ double_array := [2][4]int {[...]int{1,2,3,4}, [...]int{5,6,7,8}}
Guess what?
Since Go is about cleaner code, we can simplify this code even further:
@@ -433,7 +433,7 @@ Since Go is about cleaner code, we can simplify this code even further:
:linenos:
//über simpifikation!
- a := [2][4]int {{1,2,3,4}, {5,6,7,8}}
+ double_array := [2][4]int {{1,2,3,4}, {5,6,7,8}}
Cool, eh? Now, we can combine multiple fields of different types to create a
``struct`` and we can have ``array``\s of, as many as we want, of objects of the
View
104 control.rst
@@ -24,7 +24,7 @@ Go has the same commenting conventions as in C++. That is:
/* Everything from the beginning of this line until the keyword 'var' on
line 4 is a comment and ignored by the compiler. */
var(
- i int
+ integer int
pi = float32
prefix string
)
@@ -60,7 +60,7 @@ In Go, you don't need parenthesis for the condition.
.. code-block:: go
:linenos:
- if x>10{
+ if x > 10 {
fmt.Println("x is greater than 10")
} else {
fmt.Println("x is less than 10")
@@ -71,8 +71,8 @@ You can also have a leading initial short statement before the condition too.
.. code-block:: go
:linenos:
- // compute the value of x, and then compare.
- if x := computed_value(); x>10{
+ // Compute the value of x, and then compare it to 10.
+ if x := computed_value(); x > 10 {
fmt.Println("x is greater than 10")
} else {
fmt.Println("x is less than 10")
@@ -83,12 +83,12 @@ You can combine multiple if/else statements.
.. code-block:: go
:linenos:
- if i == 3 {
- fmt.Println("i is equal to 3")
- } else if i < 3{
- fmt.Println("i is less than 3")
+ if integer == 3 {
+ fmt.Println("The integer is equal to 3")
+ } else if integer < 3 {
+ fmt.Println("The integer is less than 3")
} else {
- fmt.Println("i is greater than 3")
+ fmt.Println("The integer is greater than 3")
}
The for statement
@@ -119,17 +119,20 @@ An example would be better than the previous paragraph, right? Here we go!
func main(){
sum := 0;
- for i:=0; i<10; i++{
- sum += i
+ for index:=0; index < 10 ; index++ {
+ sum += index
}
fmt.Println("sum is equal to ", sum)
}
In the code above we initalize our variable ``sum`` to 0. The ``for`` loop by
-is begun by initializing the variable ``i`` to 0 (``i:=0``).
-Next the ``for`` loop's body is executed (``sum += i``) *while* the condition ``i<10`` is
-true. At the end of each iteration the ``for`` loop will execute the ``i++``
-expression (eg. increments ``i``).
+is begun by initializing the variable ``index`` to 0 (``index:=0``).
+
+Next the ``for`` loop's body is executed (``sum += i``) *while* the condition
+``index < 10`` is true.
+
+At the end of each iteration the ``for`` loop will execute the ``index++``
+expression (eg. increments ``index``).
You might guess that the output from the previous program would be:
@@ -140,8 +143,10 @@ You might guess that the output from the previous program would be:
And you would be correct!
Because the sum is 0+1+2+3+4+5+6+7+8+9 which equals 45.
-**Question:** Is it possible to combine lines 5 and 6 into a single one, in the
-previous program? How?
+**Question:**
+
+ Is it possible to combine lines 5 and 6 into a single one, in the previous
+ program? How?
Actually ``expression1``, ``expression2``, and ``expression3`` are all optional.
This means you can omit, one, two or all of them in a ``for`` loop. If you omit
@@ -190,11 +195,11 @@ condition expressed by ``expression2`` is still true.
.. code-block:: go
:linenos:
- for i:=10; i>0; i--{
- if i<5{
+ for index := 10; index>0; index-- {
+ if index < 5{
break
}
- fmt.Println("i")
+ fmt.Println(index)
}
The previous snippet says: loop from 10 to 0 printing the numbers (line 6); but
@@ -207,16 +212,16 @@ Hence, the program will print the numbers: 10, 9, 8, 7, 6, 5.
.. code-block:: go
:linenos:
- for i:=10; i>0; i--{
- if i==5{
+ for index := 10; index > 0; index-- {
+ if index == 5 {
continue
}
- fmt.Println("i")
+ fmt.Println(index)
}
Here the program will print all the numbers from 10 down to 1, except 5! Because
-on line 3, the condition ``if i==5`` will be ``true`` so ``continue`` will be
-executed and the ``fmt.Println(i) (for i == 5)`` won't be run.
+on line 3, the condition ``if index == 5`` will be ``true`` so ``continue``
+will be executed and the ``fmt.Println(index) (for index == 5)`` won't be run.
The switch statement
====================
@@ -232,7 +237,7 @@ The general form of a switch statement is
.. code-block:: go
:linenos:
- //general form of a switch statement
+ // General form of a switch statement:
switch sExpr {
case expr1:
some instructions
@@ -273,7 +278,7 @@ Some switch examples:
.. code-block:: go
:linenos:
- //simple switch example
+ // Simple switch statement example:
i := 10
switch i {
case 1:
@@ -286,25 +291,25 @@ Some switch examples:
fmt.Println("All I know is that i is an integer")
}
-In this snippet, we initialized ``i`` to 10, but in practice, you should think
-of ``i`` as a computed value (result of a function or some other calculus before
-the switch statement)
+In this snippet, we initialized ``index`` to 10, but in practice, you should
+think of ``index`` as a computed value (result of a function or some other
+calculus before the switch statement)
Notice that in line 6, we grouped some expressions (2, 3, and 4) in a single
case statement.
.. code-block:: go
:linenos:
- //switch example without sExpr
- i := 10
+ // Switch example without sExpr
+ index := 10
switch {
- case i<10:
- fmt.Println("i is less than 10")
- case i>10, i<0:
- fmt.Println("i is either bigger than 10 or less than 0")
- case i==10:
- fmt.Println("i is equal to 10")
+ case index < 10:
+ fmt.Println("The index is less than 10")
+ case index > 10, index < 0:
+ fmt.Println("The index is either bigger than 10 or less than 0")
+ case index == 10:
+ fmt.Println("The index is equal to 10")
default:
fmt.Println("This won't be printed anyway")
}
@@ -313,38 +318,39 @@ Now, in this example, we omitted ``sExpr`` of the general form. So the cases
expressions should be of type *bool*. And so they are! (They're comparisons that
return either ``true`` or ``false``)
-**Question** Can you tell why the ``default`` case on line 10 will never be
-reached?
+**Question**:
+
+ Can you tell why the ``default`` case on line 10 will never be reached?
.. code-block:: go
:linenos:
//switch example with fallthrough
- i := 6
- switch i {
+ integer := 6
+ switch integer {
case 4:
- fmt.Println("was <= 4")
+ fmt.Println("The integer was <= 4")
fallthrough
case 5:
- fmt.Println("was <= 5")
+ fmt.Println("The integer was <= 5")
fallthrough
case 6:
- fmt.Println("was <= 6")
+ fmt.Println("The integer was <= 6")
fallthrough
case 7:
- fmt.Println("was <= 7")
+ fmt.Println("The integer was <= 7")
fallthrough
case 8:
- fmt.Println("was <= 8")
+ fmt.Println("The integer was <= 8")
fallthrough
default:
fmt.Println("default case")
}
In this example, the case on line 10 matches, but since there is
-``fallthrough``, the code for ``case 7:`` and so on will also be executed (just
-like a C switch that doesn't use the ``break`` keyword).
+``fallthrough``, the code for ``case 7:`` and so on will also be executed
+(just like a C switch that doesn't use the ``break`` keyword).
.. [#f1] http://golang.org/doc/go_tutorial.html#tmp_33
View
48 even-more-functions.rst
@@ -50,16 +50,16 @@ Some examples to illustrate this:
.. code-block:: go
:linenos:
- //twoOne is the type of functions with two int paarmeters and an int result.
+ // twoOne is the type of functions with two int parameters and an int result.
// max(int, int) int and sum(int, int) int, for eg. are of this type.
type twoOne func(int, int) int
- //slice_transform is the type of functions that takes a slice and outputs a
- //slice.
- //invert(s []int) []int and sort(s []int) []int are of this type.
+ // slice_transform is the type of functions that takes a slice and outputs a
+ // slice.
+ // invert(s []int) []int and sort(s []int) []int are of this type.
type slice_transform func(s []int) []int
- //varbytes is the type of variadic functions that takes bytes and outputs a
+ // varbytes is the type of variadic functions that takes bytes and outputs a
// boolean.
// redundant(...byte) bool, and contains_zero(...byte) bool are of this type
type varbytes func(...byte) bool
@@ -95,26 +95,26 @@ of course... by 'odd' I meant the 'weird' ones... what is wrong with you people?
// int parameter is odd, or false if not.
// isOdd is of type func(int) bool which is what test_int is declared to be.
- func isOdd(i int) bool{
- if i%2 == 0 {
+ func isOdd(integer int) bool {
+ if integer%2 == 0 {
return false
}
return true
}
- //same comment for isEven
- func isEven(i int) bool{
- if i%2 == 0 {
+ // Same comment for isEven
+ func isEven(integer int) bool {
+ if integer%2 == 0 {
return true
}
return false
}
- // we could've written:
- // func filter(s []int, f func(int) bool) []int
- func filter(s []int, f test_int) []int{
+ // We could've written:
+ // func filter(slice []int, f func(int) bool) []int
+ func filter(slice []int, f test_int) []int {
var result []int
- for _, value := range s{
+ for _, value := range slice {
if f(value) {
result = append(result, value)
}
@@ -123,12 +123,12 @@ of course... by 'odd' I meant the 'weird' ones... what is wrong with you people?
}
func main(){
- s := []int {1, 2, 3, 4, 5, 7}
- fmt.Println("s = ", s)
- o := filter(s, isOdd)
- fmt.Println("Odd elements of s are: ", o)
- e := filter(s, isEven)
- fmt.Println("Even elements of s are: ", e)
+ slice := []int {1, 2, 3, 4, 5, 7}
+ fmt.Println("slice = ", slice)
+ odd := filter(slice, isOdd)
+ fmt.Println("Odd elements of slice are: ", odd)
+ even := filter(slice, isEven)
+ fmt.Println("Even elements of slice are: ", even)
}
Output:
@@ -225,15 +225,15 @@ and produces two slices:
package main
import "fmt"
- func isOdd(i int) bool{
- if i%2 == 0 {
+ func isOdd(integer int) bool{
+ if integer%2 == 0 {
return false
}
return true
}
- func isBiggerThan4(i int) bool{
- if i>4 {
+ func isBiggerThan4(integer int) bool{
+ if integer > 4 {
return true
}
return false
View
79 functions.rst
@@ -17,7 +17,7 @@ two integers A and B. That's easy to do with a simple *if* statement, right?
:linenos:
//compare A and B and say wich is bigger
- if A>B {
+ if A > B {
fmt.Print("The max is A")
} else {
fmt.Print("The max is B")
@@ -109,7 +109,7 @@ The general syntax of a function is:
.. code-block:: go
:linenos:
- func funcname(input1 type1, input2 type2) (output1 type1, output2 type2){
+ func funcname(input1 type1, input2 type2) (output1 type1, output2 type2) {
//some code and processing here
...
//return the results of the function
@@ -140,19 +140,18 @@ A simple Max function
import "fmt"
//return the maximum between two int a, and b.
- func max(a, b int) int{
- if a>b {
+ func max(a, b int) int {
+ if a > b {
return a
}
return b
}
- func main(){
+ func main() {
x := 3
y := 4
z := 5
-
max_xy := max(x, y) //calling max(x, y)
max_xz := max(x, z) //calling max(x, z)
@@ -188,11 +187,11 @@ A function with two ouput values
import "fmt"
//return A+B and A*B in a single shot
- func SumAndProduct(A, B int) (int, int){
+ func SumAndProduct(A, B int) (int, int) {
return A+B, A*B
}
- func main(){
+ func main() {
x := 3
y := 4
@@ -225,22 +224,22 @@ A function with a result variable
//A function that returns a bool that is set to true of Sqrt is possible
//and false when not. And the actual square root of a float64
- func MySqrt(f float64) (s float64, ok bool){
- if f>0 {
- s, ok = math.Sqrt(f), true
+ func MySqrt(f float64) (squareroot float64, ok bool){
+ if f > 0 {
+ squareroot, ok = math.Sqrt(f), true
} else {
- s, ok = 0, false
+ squareroot, ok = 0, false
}
- return s, ok
+ return squareroot, ok
}
- func main(){
- for i:= -2.0; i<=10; i++{
- possible, sqroot := MySqrt(i)
- if possible{
- fmt.Printf("The square root of %f is %f\n", i, sqroot)
+ func main() {
+ for index := -2.0; index <= 10; i++ {
+ squareroot, possible := MySqrt(index)
+ if possible {
+ fmt.Printf("The square root of %f is %f\n", index, squareroot)
} else {
- fmt.Printf("Sorry, no square root for %f\n", i)
+ fmt.Printf("Sorry, no square root for %f\n", index)
}
}
}
@@ -282,11 +281,11 @@ to its type, we can rewrite the previous example as follows:
import "math"
//return A+B and A*B in a single shot
- func MySqrt(f float64) (s float64, ok bool){
- if f>0 {
- s, ok = math.Sqrt(f), true
+ func MySqrt(floater float64) (squareroot float64, ok bool){
+ if floater > 0 {
+ squareroot, ok = math.Sqrt(f), true
}
- return s, ok
+ return squareroot, ok
}
The empty return
@@ -303,11 +302,11 @@ Thus, we can rewrite the previous example as follow:
import "math"
//return A+B and A*B in a single shot
- func MySqrt(f float64) (s float64, ok bool) {
- if f>0 {
- s, ok = math.Sqrt(f), true
+ func MySqrt(floater float64) (squareroot float64, ok bool) {
+ if floater > 0 {
+ squareroot, ok = math.Sqrt(f), true
}
- return //omitting the output named variables, but keeping "return"
+ return // Omitting the output named variables, but keeping the "return".
}
.. _value-reference:
@@ -328,20 +327,20 @@ An example to verify the previous paragraph:
import "fmt"
//simple function that returns 1 + its input parameter
- func add1(a int) int{
+ func add1(a int) int {
a = a+1 // we change the value of a, by adding 1 to it
return a //return the new value
}
- func main(){
+ func main() {
x := 3
- fmt.Println("x = ", x) //should print "x = 3"
+ fmt.Println("x = ", x) // Should print "x = 3"
x1 := add1(x) //calling add1(x)
- fmt.Println("x+1 = ", x1) //should print "x+1 = 4"
- fmt.Println("x = ", x) //will print "x = 3"
+ fmt.Println("x+1 = ", x1) // Should print "x+1 = 4"
+ fmt.Println("x = ", x) // Will print "x = 3"
}
.. container:: output
@@ -374,20 +373,20 @@ Let's try this.
import "fmt"
//simple function that returns 1 + its input parameter
- func add1(a *int) int{ //notice that we give it a pointer to an int!
- *a = *a+1 // we dereference and change the value pointed by a
- return *a //return the new value
+ func add1(a *int) int { // Notice that we give it a pointer to an int!
+ *a = *a+1 // We dereference and change the value pointed by a
+ return *a // Return the new value
}
- func main(){
+ func main() {
x := 3
- fmt.Println("x = ", x) //should print "x = 3"
+ fmt.Println("x = ", x) // Will print "x = 3"
- x1 := add1(&x) //calling add1(&x) by passing the adress of x to it
+ x1 := add1(&x) // Calling add1(&x) by passing the adress of x to it
- fmt.Println("x+1 = ", x1) //should print "x+1 = 4"
- fmt.Println("x = ", x) //will print "x = 4"
+ fmt.Println("x+1 = ", x1) // Will print "x+1 = 4"
+ fmt.Println("x = ", x) // Will print "x = 4"
}
.. container:: output
View
5 interfaces.rst
@@ -253,9 +253,8 @@ As you may have noticed, interfaces types are abstract in that they don't
themselves implement a given and precise data structure or method.
They simply say: "if something can do *this*, it may be used *here*".
-Also, take note that types implementing a given interface do so by simply
-implementing the methods specified by the interface, they don't explicitly claim
-that they implement that interface.
+Notice that these types make *no mention* of any interface.
+Their implementation doesn't explicitly mention a given interface.
Similarly, an interface doesn't specify or even care which types implement it.
Look how ``Men`` made no mention of types ``Student`` or ``Employee``.
View
68 maps.rst
@@ -33,7 +33,7 @@ For example:
.. code-block:: go
:linenos:
- // a map that associates strings to int
+ // A map that associates strings to int
// eg. "one" --> 1, "two" --> 2...
var numbers map[string] int //declare a map of strings to ints
@@ -49,7 +49,7 @@ When used with maps, ``make`` takes an optional capacity parameter.
.. code-block:: go
:linenos:
- // a map that associates strings to int
+ // A map that associates strings to int
// eg. "one" --> 1, "two" --> 2...
var numbers map[string] int //declare a map of strings to ints
numbers = make(map[string]int)
@@ -58,7 +58,7 @@ When used with maps, ``make`` takes an optional capacity parameter.
numbers["trois"] = 3 //trois is "three" in french. I know that you know.
//...
fmt.Println("Trois is the french word for the number: ", numbers[3])
- // Trois is the french word for the number: 3
+ // Trois is the french word for the number: 3. Also a good time.
We now have the idea: it's like a table with two columns: in the left column we
have the key, and on the right column we have its *associated* value.
@@ -111,9 +111,9 @@ Literal values of maps can be expressed using a list of colon-separated
.. code-block:: go
:linenos:
- //A map representing the rating given to some programming languages.
+ // A map representing the rating given to some programming languages.
rating := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
- //This is equivalent to writing more verbosely
+ // This is equivalent to writing more verbosely
var rating = map[string]float32
rating = make(map[string]float)
rating["C"] = 5
@@ -191,11 +191,11 @@ In fact, you just have to assign any given value followed by comma ``false``.
.. code-block:: go
:linenos:
- //A map representing the rating given to some programming languages.
+ // A map representing the rating given to some programming languages.
rating := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
- map["C++"] = 1, false //we delete the entry with key "C++"
+ map["C++"] = 1, false // We delete the entry with key "C++"
cpp_rating, ok := rating["C++"]
- //would print: We have no rating associated with C++ in the map
+ // Would print: We have no rating associated with C++ in the map
if ok {
fmt.Println("C++ is in the map and its rating is ", cpp_rating)
} else {
@@ -224,7 +224,7 @@ This syntax is as follow:
:linenos:
for key, value := range m {
- // in each iteration of this loop, the variables key and value are set
+ // In each iteration of this loop, the variables key and value are set
// to the current key/value in the map
...
}
@@ -238,10 +238,10 @@ Let's see a complete example to understand this better.
import "fmt"
func main(){
- //declare a map literal
+ // Declare a map literal
ratings := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
- //iterate over the ratings map
+ // Iterate over the ratings map
for key, value := range ratings {
fmt.Printf("%s language is rated at %g\n", key, value)
}
@@ -266,12 +266,12 @@ If we don't need the value in our for statement, we can omit it like this:
import "fmt"
func main(){
- //declare a map literal
+ // Declare a map literal.
ratings := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
fmt.Print("We rated these languages: ")
- //iterate over the ratings map, and print the languages names
+ // Iterate over the ratings map, and print the languages names.
for key := range ratings {
fmt.Print(key, ",")
}
@@ -298,31 +298,31 @@ Let's rewrite a previous example using this new tool:
package main
import "fmt"
- //return the biggest value in a slice of ints
- func Max(s []int) int { //the input parameter is a slice of ints
- max := s[0] //the first element is the max for now
- for i, value := range s { //notice how we iterate!
- if value>max { //we found a bigger value in our slice
+ // Return the biggest value in a slice of ints.
+ func Max(slice []int) int { // The input parameter is a slice of ints.
+ max := slice[0] //the first element is the max for now.
+ for index, value := range slice { // Notice how we iterate!
+ if value > max { // We found a bigger value in our slice.
max = value
}
}
return max
}
func main() {
- //declare three arrays of different sizes, to test the function Max
+ // Declare three arrays of different sizes, to test the function Max.
A1 := [10]int {1,2,3,4,5,6,7,8,9}
A2 := [4]int {1,2,3,4}
A3 := [1]int {1}
//declare a slice of ints
var slice []int
- slice = A1[:] //take all A1 elements
+ slice = A1[:] // Take all A1 elements.
fmt.Println("The biggest value of A1 is", Max(slice))
- slice = A2[:] //take all A2 elements
+ slice = A2[:] // Ttake all A2 elements.
fmt.Println("The biggest value of A2 is", Max(slice))
- slice = A3[:] //take all A3 elements
+ slice = A3[:] // Ttake all A3 elements.
fmt.Println("The biggest value of A3 is", Max(slice))
}
@@ -352,11 +352,11 @@ We could have written the ``Max(s []int)int`` function like this:
.. code-block:: go
:linenos:
- //return the biggest value in a slice of ints
- func Max(s []int) int { //the input parameter is a slice of ints
- max := s[0]
- for _, value := range s { //notice how we use _ to "ignore" the index
- if value>max {
+ // Return the biggest value in a slice of ints.
+ func Max(slice []int) int { // The input parameter is a slice of ints.
+ max := slice[0]
+ for _, value := range slice { // Notice how we use _ to "ignore" the index.
+ if value > max {
max = value
}
}
@@ -370,15 +370,15 @@ identifier*.
.. code-block:: go
:linenos:
- //A function that returns a bool that is set to true of Sqrt is possible
- //and false when not. And the actual square root of a float64
- func MySqrt(f float64) (s float64, ok bool) {
- if f>0 {
- s, ok = math.Sqrt(f), true
+ // A function that returns a bool that is set to true of Sqrt is possible
+ // and false when not. And the actual square root of a float64
+ func MySqrt(floater float64) (squareroot float64, ok bool) {
+ if floater > 0 {
+ squareroot, ok = math.Sqrt(f), true
} else {
- s, ok = 0, false
+ squareroot, ok = 0, false
}
- return s, ok
+ return squareroot, ok
}
//...
r,_ = MySqrt(v) //retrieve the square root of v, and ignore its faisability
View
140 more-functions.rst
@@ -52,20 +52,21 @@ Let's see an example:
package main
import "fmt"
- //Our struct representing a person
- type person struct{
+ // Our struct representing a person
+ type person struct {
name string
age int
}
- //Return true, and the older person in a group of persons
- //Or false, and nil if the group is empty.
- func Older(people ...person) (bool, person){ //variadic function
- if(len(people) == 0){return false, person{}} //the group is empty
- older := people[0] //The first one is the older FOR NOW.
- //loop through the slice people
- for _, value := range people{ //we don't need the index
- if value.age > older.age{ //compare the current person's age with the older one
+ // Return true, and the older person in a group of persons
+ // Or false, and nil if the group is empty.
+ func Older(people ...person) (bool, person) { // Variadic function.
+ if(len(people) == 0){return false, person{}} // The group is empty.
+ older := people[0] // The first one is the older FOR NOW.
+ // Loop through the slice people.
+ for _, value := range people{ // We don't need the index.
+ // Compare the current person's age with the oldest one so far
+ if value.age > older.age {
older = value //if value is older, replace older
}
}
@@ -74,32 +75,32 @@ Let's see an example:
func main(){
- //two variables to be used by our program
+ // Two variables to be used by our program.
var(
ok bool
older person
)
- // declare some persons
+ // Declare some persons.
paul := person{"Paul", 23};
jim := person{"Jim", 24};
sam := person{"Sam", 84};
rob := person{"Rob", 54};
karl := person{"Karl", 19};
- //who is older? Paul or Jim?
+ // Who is older? Paul or Jim?
_, older = Older(paul, jim) //notice how we used the blank identifier
fmt.Println("The older of Paul and Jim is: ", older.name)
- //who is older? Paul, Jim or Sam?
+ // Who is older? Paul, Jim or Sam?
_, older = Older(paul, jim, sam)
fmt.Println("The older of Paul, Jim and Sam is: ", older.name)
- //who is older? Paul, Jim , Sam or Rob?
+ // Who is older? Paul, Jim , Sam or Rob?
_, older = Older(paul, jim, sam, rob)
fmt.Println("The older of Paul, Jim, Sam and Rob is: ", older.name)
- //who is older in a group containing only Karl?
+ // Who is older in a group containing only Karl?
_, older = Older(karl)
fmt.Println("When Karl is alone in a group, the older is: ", older.name)
- //is there an older person in an empty group?
+ // Is there an older person in an empty group?
ok, older = Older() //this time we use the boolean variable ok
if !ok {
fmt.Println("In an empty group there is no older person")
@@ -144,40 +145,40 @@ Example:
import "fmt"
func main(){
- s := []int {1, 2, 3}
+ slice := []int {1, 2, 3}
fmt.Println("At first: ")
- fmt.Println("s = ", s)
- fmt.Println("len(s) = ", len(s))
+ fmt.Println("slice = ", slice)
+ fmt.Println("len(slice) = ", len(slice))
fmt.Println("Let's append 4 to it")
- s = append(s, 4)
- fmt.Println("s = ", s)
- fmt.Println("len(s) = ", len(s))
+ slice = append(slice, 4)
+ fmt.Println("slice = ", slice)
+ fmt.Println("len(slice) = ", len(slice))
fmt.Println("Let's append 5 and 6 to it")
- s = append(s, 5, 6)
- fmt.Println("s = ", s)
- fmt.Println("len(s) = ", len(s))
+ slice = append(slice, 5, 6)
+ fmt.Println("slice = ", slice)
+ fmt.Println("len(slice) = ", len(slice))
fmt.Println("Let's append 7, 8, and 9 to it")
- s = append(s, 7, 8, 9)
- fmt.Println("s = ", s)
- fmt.Println("len(s) = ", len(s))
+ slice = append(slice, 7, 8, 9)
+ fmt.Println("slice = ", slice)
+ fmt.Println("len(slice) = ", len(slice))
}
Output:
.. container:: output
| At first:
- | s = [1 2 3]
- | len(s) = 3
+ | slice = [1 2 3]
+ | len(slice) = 3
| Let's append 4 to it
- | s = [1 2 3 4]
- | len(s) = 4
+ | slice = [1 2 3 4]
+ | len(slice) = 4
| Let's append 5 and 6 to it
- | s = [1 2 3 4 5 6]
- | len(s) = 6
+ | slice = [1 2 3 4 5 6]
+ | len(slice) = 6
| Let's append 7, 8, and 9 to it
- | s = [1 2 3 4 5 6 7 8 9]
- | len(s) = 9
+ | slice = [1 2 3 4 5 6 7 8 9]
+ | len(slice) = 9
You can even give a variadic function a slice of type ``[]T`` instead of a list
of comma-separated parameters of type ``T``.
@@ -187,11 +188,11 @@ Example:
.. code-block:: go
:linenos:
- //two slices of ints
- a := []int {1, 2, 3}
- b := []int {10, 11, 12}
- a = append(a, b...) // look at this syntax: the slice followed by three dots
- // a == {1, 2, 3, 10, 11, 12}
+ // Two slices of ints
+ a_slice := []int {1, 2, 3}
+ b_slice := []int {10, 11, 12}
+ a_slice = append(a_slice, b_slice...) // Note the syntax: the slice followed by three dots
+ // a_slice == {1, 2, 3, 10, 11, 12}
Again, just in case you glossed over the comment, notice the ellipses '``...``'
immediately following the second slice argument to the variadic function.
@@ -225,32 +226,32 @@ given slice of ``int``\s and see how the ``append`` function can help.
}
func main(){
- s := []int {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
+ slice := []int {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fmt.Println("In the beginning...")
- fmt.Println("s = ", s)
+ fmt.Println("slice = ", slice)
fmt.Println("Let's delete the first element")
- s = delete(0, s)
- fmt.Println("s = ", s)
+ slice = delete(0, slice)
+ fmt.Println("slice = ", slice)
fmt.Println("Let's delete the last element")
- s = delete(len(s)-1, s)
- fmt.Println("s = ", s)
+ slice = delete(len(slice)-1, slice)
+ fmt.Println("slice = ", slice)
fmt.Println("Let's delete the 3rd element")
- s = delete(2, s)
- fmt.Println("s = ", s)
+ slice = delete(2, slice)
+ fmt.Println("slice = ", slice)
}
Output:
.. container:: output
| In the beginning...
- | s = [1 2 3 4 5 6 7 8 9 10]
+ | slice = [1 2 3 4 5 6 7 8 9 10]
| Let's delete the first element
- | s = [2 3 4 5 6 7 8 9 10]
+ | slice = [2 3 4 5 6 7 8 9 10]
| Let's delete the last element
- | s = [2 3 4 5 6 7 8 9]
+ | slice = [2 3 4 5 6 7 8 9]
| Let's delete the 3rd element
- | s = [2 3 5 6 7 8 9]
+ | slice = [2 3 5 6 7 8 9]
The lines 6 and 7 of our program are fairly easy to understand. Aren't they?
We re-slice our slice omitting the first and the last elements respectively.
@@ -449,26 +450,26 @@ Let's write it:
import "fmt"
func Invert(slice []int){
- l := len(slice)
- if l > 1 { //only a slice of 2 or more elements can be inverted
- slice[0], slice[l-1] = slice[l-1], slice[0] //swap first and last ones
- Invert(slice[1:l-1]) //invert the slice in between
+ length := len(slice)
+ if length > 1 { // Only a slice of 2 or more elements can be inverted
+ slice[0], slice[length-1] = slice[length-1], slice[0] // Swap first and last ones
+ Invert(slice[1:length-1]) // Invert the slice in between
}
}
func main(){
- s := []int {1, 2, 3, 4, 5}
- fmt.Println("s = ", s)
- Invert(s)
- fmt.Println("Invert(s) = ", s)
+ slice := []int {1, 2, 3, 4, 5}
+ fmt.Println("slice = ", slice)
+ Invert(slice)
+ fmt.Println("Invert(slice) = ", slice)
}
Output:
.. container:: output
- | s = [1 2 3 4 5]
- | Invert(s) = [5 4 2 3 1]
+ | slice = [1 2 3 4 5]
+ | Invert(slice) = [5 4 2 3 1]
**Exercise** Recursive absurdity! The SUM of the ints in a given slice is equal
to the first element *plus* the SUM of the subslice starting from the second
@@ -537,9 +538,10 @@ Look at this simple program from `Effective Go`_.
return string(result), nil // f will be closed if we return here.
}
- func main(){
- c, _ := Contents("/etc/hosts")
- fmt.Println(c)
+
+ func main() {
+ contents, _ := Contents("/etc/hosts")
+ fmt.Println(contents)
}
Our function ``Contents`` needs a file name as its input, and it ouputs this
@@ -592,11 +594,11 @@ Example:
package main
import "fmt"
- func A(){
+ func A() {
fmt.Println("Running function A")
}
- func B(){
+ func B() {
fmt.Println("Running function B")
}
View
270 slices.rst
@@ -27,7 +27,7 @@ You can declare a slice just like you declare an array, omitting the size.
:linenos:
//declare a slice of ints
- var a []int //notice how we didn't specify a size.
+ var array []int // Notice how we didn't specify a size.
As we see in the above snippet, we can declare a slice literal just like an
array literal, except that we leave out the size number.
@@ -36,7 +36,7 @@ array literal, except that we leave out the size number.
:linenos:
//declare a slice of ints
- s := []byte {'a', 'b', 'c', 'd'} //a slice of bytes (current size is 4)
+ slice := []byte {'a', 'b', 'c', 'd'} // A slice of bytes (current size is 4).
We can create slices by *slicing* an array or even an existing slice.
That is taking a portion (a 'slice') of it, using this syntax ``array[i:j]``.
@@ -46,14 +46,19 @@ end before **j**. (``array[j]`` not included in the slice)
.. code-block:: go
:linenos:
- //declare an array of 10 bytes (ASCII characters). Remember: byte is uint8
- var ar [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
- //declare a and b as slice of bytes
- var a,b []byte
- //make the slice "a" refer to a "portion" of "ar".
- a = ar[2:5] //a is a portion of ar that contains: ar[2], ar[3] and ar[4]
- //make the slice "b" refer to another "portion" of "ar".
- b = ar[3:5] //b is a portion of ar that contains: ar[3], ar[4]
+ // Declare an array of 10 bytes (ASCII characters). Remember: byte is uint8.
+ var array [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
+
+ // Declare a and b as slice of bytes
+ var a_slice,b_slice []byte
+
+ // Make the slice "a_slice" refer to a "portion" of "array".
+ a_slice = array[2:5]
+ // a_slice is now a portion of array that contains: array[2], array[3] and array[4]
+
+ // Make the slice "b_slice" refer to another "portion" of "array".
+ b_slice = array[3:5]
+ // b_slice is now a portion of array that contains: array[3], array[4]
.. graphviz::
@@ -98,22 +103,27 @@ concepts.
.. code-block:: go
:linenos:
- //declare an array of 10 bytes (ASCII characters). Remember: byte is uint8
- var ar [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
+ // Declare an array of 10 bytes (ASCII characters). Remember: byte is uint8
+ var array [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
//declare a and b as slice of bytes
- var a,b []byte
- //examples of slicing
- a = ar[4:8] // means: a contains elements of ar from ar[4] to ar[7]: e,f,g,h
- a = ar[6:7] // means: a contains ONE element of ar and that is ar[6]: g
- //examples of shorthands
- a = ar[:3] // means: a = ar[0:3] thus: a contains: a,b,c
- a = ar[5:] // means: a = ar[5:9] thus: a contains: f,g,h,i,j
- a = ar[:] //means: a = ar[0:9] thus: a contains all ar elements.
- //slice of a slice
- a = ar[3:7] // means: a contains elements form 3 to 6 of ar: d,e,f,g
- b = a[1:3] //means: b contains elements a[1], a[2] i.e the chars e,f
- b = a[:3] //means: b contains elements a[0], a[1], a[2] i.e the chars: d,e,f
- b : a[:] //mens: b contains all elements of slice a: d,e,f,g
+ var a_slice,b_slice []byte
+
+ // Examples of slicing
+ a_slice = array[4:8]
+ // means: a_slice contains elements of array from array[4] to array[7]: e,f,g,h
+ a_slice = array[6:7]
+ // means: a_slice contains ONE element of array and that is array[6]: g
+
+ // Examples of shorthands
+ a_slice = array[:3] // means: a_slice = array[0:3] thus: a contains: a,b,c
+ a_slice = array[5:] // means: a_slice = array[5:9] thus: a contains: f,g,h,i,j
+ a_slice = array[:] // means: a_slice = array[0:9] thus: a contains all array elements.
+
+ // Slice of a slice
+ a_slice = array[3:7] // means: a_slice contains elements form 3 to 6 of array: d,e,f,g
+ b_slice = a_slice[1:3] //means: b_slice contains elements a[1], a[2] i.e the chars e,f
+ b_slice = a_slice[:3] //means: b_slice contains elements a[0], a[1], a[2] i.e the chars: d,e,f
+ b_slice : a_slice[:] //mens: b_slice contains all elements of slice a: d,e,f,g
Does it make more sense now? Fine!
@@ -140,31 +150,31 @@ Let's see the code.
package main
import "fmt"
- //return the biggest value in a slice of ints
- func Max(s []int) int{ //the input parameter is a slice of ints
- max := s[0] //the first element is the max for now
- for i:=1; i<len(s); i++{
- if s[i]>max{ //we found a bigger value in our slice
- max = s[i]
+ // Return the biggest value in a slice of ints.
+ func Max(slice []int) int{ // The input parameter is a slice of ints.
+ max := slice[0] // The first element is the max for now.
+ for index := 1; index < len(slice); index++ {
+ if slice[index]>max{ // We found a bigger value in our slice.
+ max = slice[index]
}
}
return max
}
- func main(){
- //declare three arrays of different sizes, to test the function Max
+ func main() {
+ // Declare three arrays of different sizes, to test the function Max.
A1 := [10]int {1,2,3,4,5,6,7,8,9}
A2 := [4]int {1,2,3,4}
A3 := [1]int {1}
- //declare a slice of ints
+ // Declare a slice of ints.
var slice []int
- slice = A1[:] //take all A1 elements
+ slice = A1[:] // Take all A1 elements.
fmt.Println("The biggest value of A1 is", Max(slice))
- slice = A2[:] //take all A2 elements
+ slice = A2[:] // Take all A2 elements.
fmt.Println("The biggest value of A2 is", Max(slice))
- slice = A3[:] //take all A3 elements
+ slice = A3[:] // Take all A3 elements.
fmt.Println("The biggest value of A3 is", Max(slice))
}
@@ -207,38 +217,38 @@ Let's see an example:
func PrintByteSlice(name string, slice []byte){
fmt.Printf("%s is : [", name)
- for i:=0; i<len(slice)-1; i++{
+ for index :=0; index < len(slice)-1; index ++{
fmt.Printf("%q,", slice[i])
}
fmt.Printf("%q]\n", slice[len(slice)-1])
}
func main(){
- //declare an array of 10 bytes.
+ // Declare an array of 10 bytes.
A := [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
- //declare some slices
- slice1 := A[3:7] // slice1 == {'d', 'e', 'f', 'g'}
- slice2 := A[5:] //slice2 == {'f', 'g', 'h', 'i', 'j'}
- slice3 := slice1[:2] //slice3 == {'d', 'e'}
+ //declare some slices.
+ slice1 := A[3:7] // Slice1 == {'d', 'e', 'f', 'g'}
+ slice2 := A[5:] // Slice2 == {'f', 'g', 'h', 'i', 'j'}
+ slice3 := slice1[:2] // Slice3 == {'d', 'e'}
- //let's print the current content of A and the slices
+ // Let's print the current content of A and the slices.
fmt.Println("\nFirst content of A and the slices")
PrintByteSlice("A", A[:])
PrintByteSlice("slice1", slice1)
PrintByteSlice("slice2", slice2)
PrintByteSlice("slice3", slice3)
- //let's change the 'e' in A to 'E'
+ // Let's change the 'e' in A to 'E'.
A[4] = 'E'
fmt.Println("\nContent of A and the slices, after changing 'e' to 'E' in array A")
PrintByteSlice("A", A[:])
PrintByteSlice("slice1", slice1)
PrintByteSlice("slice2", slice2)
PrintByteSlice("slice3", slice3)
- //Let's change the 'g' in slice2 to 'G'
- slice2[1] = 'G' //it's the 2nd element in slice2!
+ // Let's change the 'g' in slice2 to 'G'.
+ slice2[1] = 'G' // Remember that 1 is actually the 2nd element in slice2!
fmt.Println("\nContent of A and the slices, after changing 'g' to 'G' in slice2")
PrintByteSlice("A", A[:])
PrintByteSlice("slice1", slice1)
@@ -365,9 +375,9 @@ Schematically, since you *love* my diagrams.
.. code-block:: go
:linenos:
- //declare an array of 10 bytes.
- A := [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
- s := A[4:8]
+ // Declare an array of 10 bytes.
+ array := [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
+ slice := A[4:8]
The picture above is a representation of a slice that starts from the 5th
element of the array. It contains exactly 4 elements (its length) and can
@@ -401,11 +411,10 @@ Example:
:linenos:
var slice1, slice2 []int
- slice1 = make([]int, 4, 4)
- //so slice1 == []int {0, 0, 0, 0}
- slice2 = make([]int``,`` 4) //cap == len == 4
- //so slice2 == []int {0, 0, 0, 0}
- //cap(slice2) == len(slice2) == 4
+ slice1 = make([]int, 4, 4) // slice1 is []int {0, 0, 0, 0}
+ slice2 = make([]int``,`` 4) // slice2 is []int {0, 0, 0, 0}
+ // Note: cap == len == 4 for slice2
+ // cap(slice2) == len(slice2) == 4
The zero value of a slice is ``nil``.
The ``len`` and ``cap`` functions will both return **0** for a ``nil`` slice.
@@ -418,37 +427,39 @@ The ``len`` and ``cap`` functions will both return **0** for a ``nil`` slice.
import "fmt"
func main() {
- var s []int
+ var slice []int
fmt.Println("Before calling make")
- if s==nil {fmt.Println("s == nil")}
- fmt.Println("len(s) == ", len(s))
- fmt.Println("cap(s) == ", cap(s))
- //let's allocate the underlying array
+ if slice==nil {fmt.Println("slice == nil")}
+ fmt.Println("len(slice) == ", len(slice))
+ fmt.Println("cap(slice) == ", cap(slice))
+
+ // Let's allocate the underlying array:
fmt.Println("After calling make")
- s = make([]int, 4)
- fmt.Println("s == ", s)
- fmt.Println("len(s) == ", len(s))
- fmt.Println("cap(s) == ", cap(s))
- //let's change things
- fmt.Println("Let's change some of its elements: s[1], s[3] = 2, 3")
- s[1], s[3] = 2, 3
- fmt.Println("s == ", s)
+ slice = make([]int, 4)
+ fmt.Println("slice == ", slice)
+ fmt.Println("len(slice) == ", len(slice))
+ fmt.Println("cap(slice) == ", cap(slice))
+
+ // Let's change things:
+ fmt.Println("Let's change some of its elements: slice[1], slice[3] = 2, 3")
+ slice[1], slice[3] = 2, 3
+ fmt.Println("slice == ", slice)
}
Output:
.. container:: output
| Before calling make
- | s == nil
- | len(s) == 0
- | cap(s) == 0
+ | slice == nil
+ | len(slice) == 0
+ | cap(slice) == 0
| After calling make
- | s == [0 0 0 0]
- | len(s) == 4
- | cap(s) == 4
- | Let's change some of its elements: s[1], s[3] = 2, 3
- | s == [0 2 0 3]
+ | slice == [0 0 0 0]
+ | len(slice) == 4
+ | cap(slice) == 4
+ | Let's change some of its elements: slice[1], slice[3] = 2, 3
+ | slice == [0 2 0 3]
We may find ourselves wondering "Why use ``make`` instead of ``new`` that we discussed
when we studied :ref:`pointers<function-new>`?"
@@ -496,64 +507,64 @@ a slice. A very simple, straight forward way of growing a slice might be:
func PrintIntSlice(name string, slice []int){
fmt.Printf("%s == [", name)
- for i:=0; i<len(slice)-1; i++{
- fmt.Printf("%d,", slice[i])
+ for index := 0; index < len(slice)-1; index++ {
+ fmt.Printf("%d,", slice[index])
}
fmt.Printf("%d]\n", slice[len(slice)-1])
}
- func GrowIntSlice(slice []int, add int) []int{
+ func GrowIntSlice(slice []int, add int) []int {
new_capacity := cap(slice)+add
new_slice := make([]int, len(slice), new_capacity)
- for i:=0; i<len(slice); i++{
- new_slice[i] = slice[i]
+ for index := 0; index < len(slice); index++ {
+ new_slice[index] = slice[index]
}
return new_slice
}
func main(){
- s := []int {0, 1, 2, 3}
+ slice := []int {0, 1, 2, 3}
fmt.Println("Before calling GrowIntSlice")
- PrintIntSlice("s", s)
- fmt.Println("len(s) == ", len(s))
- fmt.Println("cap(s) == ", cap(s))
+ PrintIntSlice("slice", slice)
+ fmt.Println("len(slice) == ", len(slice))
+ fmt.Println("cap(slice) == ", cap(slice))
- //let's call GrowIntSlice
- s = GrowIntSlice(s, 3) //add 3 elements to the slice
+ // Let's call GrowIntSlice
+ slice = GrowIntSlice(slice, 3) //add 3 elements to the slice
fmt.Println("After calling GrowIntSlice")
- PrintIntSlice("s", s)
- fmt.Println("len(s) == ", len(s))
- fmt.Println("cap(s) == ", cap(s))
-
- //Let's two elements to the slice
- //so we reslice the slice to add 2 to its original length
- s = s[:len(s)+2] //We can do this because cap(s) == 7
- s[4], s[5] = 4, 5
- fmt.Println("After adding new elements: s[4], s[5] = 4, 5")
- PrintIntSlice("s", s)
- fmt.Println("len(s) == ", len(s))
- fmt.Println("cap(s) == ", cap(s))
+ PrintIntSlice("slice", slice)
+ fmt.Println("len(slice) == ", len(slice))
+ fmt.Println("cap(slice) == ", cap(slice))
+
+ // Let's two elements to the slice
+ // So we reslice the slice to add 2 to its original length
+ slice = slice[:len(slice)+2] // We can do this because cap(slice) == 7
+ slice[4], slice[5] = 4, 5
+ fmt.Println("After adding new elements: slice[4], slice[5] = 4, 5")
+ PrintIntSlice("slice", slice)
+ fmt.Println("len(slice) == ", len(slice))
+ fmt.Println("cap(slice) == ", cap(slice))
}
Output:
.. container:: output
| Before calling GrowIntSlice
- | s == [0,1,2,3]
- | len(s) == 4
- | cap(s) == 4
+ | slice == [0,1,2,3]
+ | len(slice) == 4
+ | cap(slice) == 4
| After calling GrowIntSlice
- | s == [0,1,2,3]
- | len(s) == 4
- | cap(s) == 7
- | After adding new elements: s[4], s[5] = 4, 5
- | s == [0,1,2,3,4,5]
- | len(s) == 6
- | cap(s) == 7
+ | slice == [0,1,2,3]
+ | len(slice) == 4
+ | cap(slice) == 7
+ | After adding new elements: slice[4], slice[5] = 4, 5
+ | slice == [0,1,2,3,4,5]
+ | len(slice) == 6
+ | cap(slice) == 7
Let's discuss this program a little bit.
@@ -586,8 +597,8 @@ So we can replace the lines 16, 17, 18 with this very simple one:
.. code-block:: go
:linenos:
- //instead of lines 16, 17, 18 of the previous source:
- copy(new_slice, slice) //copy elements from slice to new_slice
+ // Instead of lines 16, 17, 18 of the previous source:
+ copy(new_slice, slice) // Copy elements from slice to new_slice
You know what? Let's write another example and use the ``copy`` function in it.
@@ -599,51 +610,50 @@ You know what? Let's write another example and use the ``copy`` function in it.
func PrintByteSlice(name string, slice []byte){
fmt.Printf("%s is : [", name)
- for i:=0; i<len(slice)-1; i++{
- fmt.Printf("%q,", slice[i])
+ for index := 0; index < len(slice)-1; index++ {
+ fmt.Printf("%q,", slice[index])
}
fmt.Printf("%q]\n", slice[len(slice)-1])
}
func Append(slice, data[]byte) []byte {
- l := len(slice)
- if l + len(data) > cap(slice) { // reallocate
+ length := len(slice)
+ if length + len(data) > cap(slice) { // Reallocate
// Allocate enough space to hold both slice and data
newSlice := make([]byte, l+len(data))
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
- slice = slice[0:l+len(data)]
+ slice = slice[0:length+len(data)]
- copy(slice[l:l+len(data)], data)
+ copy(slice[length:length+len(data)], data)
return slice
}
- func main(){
-
- h := []byte {'H', 'e', 'l', 'l', 'o'}
- w := []byte {' ', 'W', 'o', 'r', 'l', 'd'}
+ func main() {
+ hello := []byte {'H', 'e', 'l', 'l', 'o'}
+ world := []byte {' ', 'W', 'o', 'r', 'l', 'd'}
fmt.Println("Before calling Append")
- PrintByteSlice("h", h)
- PrintByteSlice("w", w)
+ PrintByteSlice("hello", hello)
+ PrintByteSlice("world", world)
fmt.Println("After calling Append")
- Append(h, w)
- PrintByteSlice("h", h)
- PrintByteSlice("w", w)
+ Append(hello, world)
+ PrintByteSlice("hello", hello)
+ PrintByteSlice("world", world)
}
Output:
.. container:: output
| Before calling Append
- | h is : ['H','e','l','l','o']
- | w is : ['W','o','r','l','d']
+ | hello is : ['H','e','l','l','o']
+ | world is : ['W','o','r','l','d']
| After calling Append
- | h is : ['H','e','l','l','o','W','o','r','l','d']
- | w is : ['W','o','r','l','d']
+ | hello is : ['H','e','l','l','o','W','o','r','l','d']
+ | world is : ['W','o','r','l','d']
Appending to a slice is a very common operation and as such Go has a built-in
function called ``append`` that we will see in details very soon.

0 comments on commit 76061d7

Please sign in to comment.