Go Basics: Difference between revisions

From Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:
mkdir -p $HOME/projects/go/src
mkdir -p $HOME/projects/go/src
export GOPATH=$HOME/projects/go (add this to .profile too)
export GOPATH=$HOME/projects/go (add this to .profile too)
</pre>
== Commands ==
<pre>
godoc
go build
go install
go run
</pre>
</pre>


Line 40: Line 32:
     fmt.Println("Hello", who)   
     fmt.Println("Hello", who)   
}
}
</pre>
== Commands ==
<pre>
godoc            # show docs on command line or local web server
go build        # create binary file in local directory
go install      # create binary file, install it somewhere
go run          # just run it, disposing of binary
</pre>
</pre>

Revision as of 19:38, 7 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            # show docs on command line or local web server
go build         # create binary file in local directory
go install       # create binary file, install it somewhere
go run           # just run it, disposing of binary