Go Basics: Difference between revisions

From Wiki
Jump to navigation Jump to search
Line 57: Line 57:




== Variables ==
== Constants and Variables ==
<source lang="go">
<source lang="go">
var i int       // declared, silently initialized to 0
count, err = fmt.Println(x) // get number of bytes printed and error
var i int = 56  // declared and initialized
count, _ = fmt.Println(x)  // get number of bytes printed; discard error
i := 56         // same thing, infers type from context; only allowed inside a function
_, err = fmt.Println(x)    // discard number of bytes printed; get error
fmt.Println(x)              // ignore return values
const limit = 512        // constant; type-compatible with any number
const top uint16 = 1421  // constant; type: uint16
start := -19            // variable; inferred type: int
end := int64(9876543210) // variable; type: int64
var i int               // variable; value 0; type: int
var debug = false        // variable; inferred type: bool
checkResults := true    // variable; inferred type: bool
stepSize := 1.5         // variable; inferred type: float64
acronym := "FOSS"        // variable; inferred type: string
i, j := 56, 67  // multiple assignment
i, j := 56, 67  // multiple assignment
</source>
</source>

Revision as of 00:03, 11 February 2014

Resources: http://golang.org/

Install and Setup

sudo apt-get install golang

http://golang.org/doc/code.html discusses setting up the GOLANG environment variable and your code repository

mkdir -p $HOME/projects/go/src
export GOPATH=$HOME/projects/go (add this to .profile too)

Hello World

// hello.go
package main

import (   
    "fmt"
    "os"
    "strings"
)

func main() {
    who := "World!"  
    if len(os.Args) > 1 { /* os.Args[0] is "hello" or "hello.exe" */  
        who = strings.Join(os.Args[1:], " ")  
    }
    fmt.Println("Hello", who)  
}

Commands

godoc -http=:8000                 # host docs at http://localhost:8000
go build                          # create binary file in local directory
go install                        # create binary file in $GOPATH/bin
go run                            # just run it, disposing of binary
go get github.com/nsf/gocode      # grab a remote package and install it to $GOPATH/bin
go get -u github.com/nsf/gocode   # update to latest version of remote package


Comments

// for end-of-line
/* for multiline */

Packages

  • Every file should begin with a package statement.
  • A package may be defined across several files.
  • Code execution begins with a main() function inside package main


Constants and Variables

count, err = fmt.Println(x) // get number of bytes printed and error
count, _ = fmt.Println(x)   // get number of bytes printed; discard error
_, err = fmt.Println(x)     // discard number of bytes printed; get error
fmt.Println(x)              // ignore return values
const limit = 512        // constant; type-compatible with any number
const top uint16 = 1421  // constant; type: uint16
start := -19             // variable; inferred type: int
end := int64(9876543210) // variable; type: int64
var i int                // variable; value 0; type: int
var debug = false        // variable; inferred type: bool
checkResults := true     // variable; inferred type: bool
stepSize := 1.5          // variable; inferred type: float64
acronym := "FOSS"        // variable; inferred type: string
i, j := 56, 67   // multiple assignment