How To Use Arrays and Slices in Google Go

August 5th, 2010 by Kevin | No Comments | Filed in Go Programming

Array
An array is a collection of like objects. In Google Go programming, arrays are declared as,

var arrayOfInt [10]int

The length is part of the array’s type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered using the built-in function len(a). The elements can be indexed by integer indices 0 through the len(a)-1. If the array is indexed beyond len(a)-1, then we get an out of bounds error.

In C, arrayOfInt would be usable as a pointer to int. In Go, since arrays are values, we have to use a pointer to an array. What this means is that in Google Go,

var pArray *int = &arrayOfInt //pArray is a pointer to an integer.
var pArray *[10]int = &arrayOfInt //pArray is a pointer to an array of 10 integers.

In the declaration of the pointer to an array, the size of the array is mandatory. When assigning the address of an array to a pointer variable, the & referencing is mandatory.

Arrays are values. Assigning one array to another copies all the elements. In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it. The value property can be useful but also expensive; if you want C-like behavior and efficiency, you can pass a pointer to the array.

package main 

import "fmt" 

func arrayLen (pArray *[5]int) int{
   return len(pArray)
} 

func main(){
   var arrayOfInt [5]int 

   arrayOfInt[0] = 0
   arrayOfInt[1] = 1
   arrayOfInt[2] = 2
   arrayOfInt[3] = 3
   arrayOfInt[4] = 4 

   fmt.Println("Array Length = ", arrayLen(&arrayOfInt))
}

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})

A type followed by a brace-bounded expression—is a constructor for a value, in this case an array of 3 ints.

Slice
Slices wrap arrays to give a more general, powerful, and convenient interface to sequences of data. Slices can be considered as a sub-section of an array. Slices are reference types, which means that if you assign one slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array.

We can declare a slice variable, by assigning a pointer to any array with the same element type,

var arrayOfInt [5]int
var slice []int = &arrayOfInt

or by a slice expression of the form a[low : high], representing the subarray indexed by low through high-1.

var arrayOfInt [5]int
slice = arrayOfInt[1:3]

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; multiple arrays can never share data.

When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.

Using slices one can write the earlier program as:

package main

import "fmt"

func arrayLen (slice []int) int{
   return len(slice)
}

func main(){
   var arrayOfInt [5]int

   arrayOfInt[0] = 0
   arrayOfInt[1] = 1
   arrayOfInt[2] = 2
   arrayOfInt[3] = 3
   arrayOfInt[4] = 4

   fmt.Println("Array Length = ", arrayLen(&arrayOfInt))

}

We pass the pointer to arrayLen() by (implicitly) promoting it to a slice.

Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len(s); unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

 

Related Posts:

Tags:

How To Define Functions in Google Go

August 2nd, 2010 by Kevin | No Comments | Filed in Go Programming

A program can be broken down into modules that each perform a specific function. These modules interact with each other to perform the entire functionality of the program. Functions are the basis of modular programming in Google Go.

In Google Go, a function is declared using the keyword func as shown below,

func [function name] ([param1 declaration], [param2 declaration],...) (ret1 declaration, ret2 declaration, ...){
     //body of the function
}

The function arguments declaration (param1) and return values declaration (ret1) is similar to variable declaration except the var keyword is not used. (See How To Define Variables In Google Go)

func min(x int, y int) int {
        if x < y {
             return x
        }
        return y
}

In Google Go, it is necessary that the opening brace { of the scope should be placed on the same line as the function declaration else you will get a compilation error.

func main()
{                                          //Wrong
   fmt.Println("Hello World")
}

func main() {                         //Correct
   fmt.Println("Hello World")
}

One of Go's unusual features is that functions and methods can return multiple values. Multiple return values are declared after the argument list and separated by a comma as shown below.

package main

import "fmt"

func calc(a int, b int) (add int, sub int){
     add = a + b
     sub = a - b
     return
}

func main(){
     a := 100
     b := 50
     add, sub := calc(a, b)
     fmt.Println("Addition = ", add);
     fmt.Println("Subtraction = ", sub);
}

The return or result "parameters" of a Go function can be given names and used as regular variables, just like the incoming parameters. When named, they are initialized to the zero values for their types when the function begins; if the function executes a return statement with no arguments, the current values of the result parameters are used as the returned values.

Defer
Go's defer statement schedules a function call to be run immediately before the function executing the defer returns. It's an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a file.

// Contents returns the file's contents as a string.
func Contents(filename string) (string, os.Error) {
    f, err := os.Open(filename, os.O_RDONLY, 0)
    if err != nil {
        return "", err
    }
    defer f.Close()  // f.Close will run when we're finished.

    var result []byte
    buf := make([]byte, 100)
    for {
        n, err := f.Read(buf[0:])
        result = bytes.Add(result, buf[0:n])
        if err != nil {
            if err == os.EOF {
                break
            }
            return "", err  // f will be closed if we return here.
        }
    }
    return string(result), nil // f will be closed if we return here.
}

Deferring a function like this has two advantages. First, it guarantees that you will never forget to close the file, a mistake that's easy to make if you later edit the function to add a new return path. Second, it means that the close sits near the open, which is much clearer than placing it at the end of the function.

The arguments to the deferred function are evaluated when the defer executes, not when the call executes. Besides avoiding worries about variables changing values as the function executes, this means that a single deferred call site can defer multiple function executions.

Here's a silly example.

for i := 0; i < 5; i++ {
    defer fmt.Printf("%d ", i)
}

Deferred functions are executed in LIFO order, so this code will cause 4 3 2 1 0 to be printed when the function returns.

 

Related Posts:

Tags:

How To Use Strings In Google Go

July 31st, 2010 by Kevin | 2 Comments | Filed in Go Programming

In the earlier tutorials, we have seen how to define variables and how to define constants in Google Go. This tutorial explains how we can use strings in Google Go programming.

Strings
The predeclared string type is string. Unlike C/C++ programming, Strings are length-delimited not NUL-terminated. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. In C++ terms, Go strings are a bit like const strings, while pointers to strings are analogous to const string references. Once you’ve built a string value, you can’t change it, although of course you can change a string variable simply by reassigning it.

This snippet from strings.go is legal code:

s := "hello"
if s[1] != 'e' { os.Exit(1) }
s = "good bye"
var p *string = &s
*p = "ciao"

However the following statements are illegal because they would modify a string value:

s[0] = 'x'
(*p)[1] = 'y'

The elements of strings have type byte and may be accessed using the usual indexing operations. It is illegal to take the address of such an element; if s[i] is theith byte of a string, &s[i] is invalid.

String literals
A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.

Raw string literals are character sequences between back quotes ` `. Within the quotes, any character is legal except back quote. When using back quotes, backslashes have no special meaning and the string may span multiple lines.

Interpreted string literals are character sequences between double quotes ” “. The text between the quotes, which may not span multiple lines, forms the value of the literal, with backslash escapes interpreted as they are in character literals.

Below are some examples of both types of string literals. Google Go also supports Unicode strings.

`abc`  // same as "abc"
`\n
\n`    // same as "\\n\n\\n"
"\n"
""
"Hello, world!\n"
"日本語"                                 // UTF-8 input text
`日本語`                                 // UTF-8 input text as a raw literal

Strings can be concatenated using the ‘+’ operator. The length of string s can be discovered using the built-in function len(). The length is a compile-time constant if s is a string literal.

var string1 = "Hello World "
var string2 = "This is Google Go"
string3 := string1 + string2	//string3 = "Hello World This is Google Go"
len_string3 := len(string3)	//len_string3 = 29

Google Go provides a “strings” package that consists of several functions to manipulate strings. You can find more information at http://golang.org/pkg/strings/.

 

Related Posts:

Tags: