Author Topic: Setting up you Golang Environment  (Read 2309 times)

0 Members and 1 Guest are viewing this topic.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Setting up you Golang Environment
« on: February 13, 2015, 05:21:03 pm »
Starting Golang programming- Setting up Go environment

Contents:
0x00 - Golang
0x01 - Software
0x02 - Setting up Environment variables
0x03 - Starting Golang programming

0x00 - Golang
Golang - The Go programming language is an open source project to make programmers more productive. Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multi core and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language. Go can be used for alot of scenarios; as a system programming language, for web publishing, on the android platform and many more uses a mature programming language is expected to have.

Documentation
There is a substatial amount of documentation written about Go. The Go Tutorial, Effective Go Document and http://golang.org/doc/ are a very good place to start reading up on Go. Starting from Go v1, Go comes with its own documentation and can be viewed using:
Code: [Select]
go doc builtin
As you go on, you will realise alot of resembrance between Golang and Erlang. Notable differences between Erlang and Go is that Erlang borders on being a functional language, where Go is an imperative one. And Erlang runs in a virtual machine, while Go is compiled. Go also has a much more Unix-like feel to it.

0x01 - Software
In order to compile any golang source code, you are gonna need the go toolchain installed on the system. After compiling the code should run on w/o the go tools present on the system, or on any system if cross-compiling is used.

Windows:

Ok, yeah-yah. I will start with you b'se as always, everything is simplified for you. Point your browser to http://golang.org/dl/ and get the latest installer. Click, click and done. You have a go environment. Afew things might not work yet b'se some few environment variables ain't setup yet. For windows environment variable, check out http://golang.org/doc/install#windows for details.

OSX:
Here the easiest way is to use homebrew throurh your Terminal.app:
Code: [Select]
$brew install go --cross-compile-commonAll should be done except for afew things like setting up environment variables and VCS setup.

Linux:
Well a simple sudo apt-get install go , sudo yum install go or any specific package manager install command of you system should do it. Be happy i saved you the installing from source part though i know Gentoo people will still do it.

0x02 - Setting up Environment variables
By convention, all your Go code and the code you will import, will live inside a workspace. This convention might seem rigid at first, but it quickly becomes clear that such a convention (like most Go conventions) makes our life much easier. So lets tell GO where our workspace is. The commands should be the same on most systems except for windows users who should substitute 'export' with 'set':

Let's create a folder named "go" and make this our workspace.
Code: [Select]
$mkdir $HOME/go
$export GOPATH=$HOME/go
Problem is that on the next system restart, Go won't know where our workspace is. So lets put this in our profile. You can use your bashrc or bash profile.
Code: [Select]
$vi $HOME/.bash_profile
or
$vi ~/.bashrc
You can always use your favorite editor other than vim. Now add these lines to your profile:
Code: [Select]
#export GOROOT=$HOME/go
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
export GOOS=linux
It is always a good practice not to set your GOROOT the same as your GOPATH, that is why i commented out GOROOT [Don't remember where i read that].

Setting up Version Control System tools:
If you are an avid developer, most of these should already be on your system but it doesn't hurt to check. Lucky, they should be in your package manager already and windows users can download their binaries and install them.
Code: [Select]
sudo apt-get install mercurial hg git bzr
or
sudo yum install mercurial hg bzr git
With these setup, it is very easy to get third-party libraries with go. You should now use your new tools to install afew extra tools for go:
Code: [Select]
$ go get code.google.com/p/go.tools/cmd/godoc
$ go get github.com/golang/lint/golint
Enough with this boring stuff, lets get some code going.

0x03 - Start Go programming: Get your feet wet.
"leave your object oriented brain at home. Embrace the interface." - @mikege-
hard


Go programming is fairly easy, create a .go file, put code into it, compile and lose the code. You are good to Go.

Our generic Hello_world.go
Code: (go) [Select]
package main

import "fmt"  // Implements formatted I/O.

/* Print something */
func main() {
fmt.Printf("Hello, world")
}
Now execute this to run a copy without an executable in $GOBIN:
Code: [Select]
go run hello_world.goor
Code: [Select]
$go install hello_world.gothen
Code: [Select]
$GOBIN/hello_world
I would advise you to grab a Golang book in the ebook section or pick from alot of free books the Go community provides on the webs. Otherwise the Go documentation is enough on its own coupled with the Go tour
« Last Edit: February 13, 2015, 05:25:17 pm by kenjoe41 »
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Spacecow

  • Guest
Re: Setting up you Golang Environment
« Reply #1 on: February 15, 2015, 04:06:36 am »
Code: [Select]
#!/bin/sh -u
#
# Author: Jacques Pharand
# Date: 13/08/2014
# License: The MIT License (MIT)
# Description:
#   A little ugly duckly script that sets up a GOPATH in the user's home directory.
#   - Creates $HOME/go, $HOME/go/bin, $HOME/go/pkg and $HOME/go/src
#   - Attempts to add $GOPATH to users shell config file
#   - Offers to create a github directory under $GOPATH/src
#   - Finally tests whether go is properly installed and will run a simple test program


if [ $# -gt 0 ]; then
    echo "$0: Set up a $GOPATH on a Unix workstation."
    exit 1
fi

echo "[*] Setting up GOPATH in $HOME directory"  # Creating directories for GOPATH under $HOME
mkdir $HOME/go 2>/dev/null
mkdir $HOME/go/bin 2>/dev/null
mkdir $HOME/go/pkg 2>/dev/null
mkdir $HOME/go/src 2>/dev/null

if [ $SHELL = '/bin/bash' ]; then  # Attempting to add GOPATH variable to shell config file.
    if [ -f "$HOME/.bashrc" ]; then
        echo 'GOPATH=$HOME/go' >> $HOME/.bashrc
    else
        echo '[!] Could not find .bashrc, GOPATH variable must be added manually'
        sleep 2
    fi
elif [ $SHELL = '/bin/zsh' ]; then
    if [ -f "$HOME/.zshrc" ]; then
        echo 'GOPATH=$HOME/go' >> $HOME/.zshrc
    else
        echo '[!] Could not find .zshrc, GOPATH variable must be added manually'
        sleep 2
    fi
else
    echo "[!] Unknown shell found in \$SHELL, GOPATH variable must be added manually."
    sleep 2
fi

read -p "Would you like to add a github account to your GOPATH? [y/N] " github  # Offer to create a github directory for user's account

if [ $github = 'Y' -o $github = 'y' ]; then
    read -p "Enter your github user account name: " acct
    echo "[*] Creating directory $GOPATH/src/github.com/$acct"
    sleep 2
    mkdir -p $HOME/go/src/github.com/$acct
else
    echo "[!] Skipping account creation"
    sleep 2
fi

echo "[*] Testing Go and GOPATH installation"  # Attempt to test go installation and compile a test program
sleep 2
go version 1>/dev/null 2>&1

if [ $? -ne 0 ]; then
    echo "[!] Go is either not installed or not in PATH"
    sleep 2
else
    mkdir $HOME/go/src/helloworld 2>/dev/null
    touch $HOME/go/src/helloworld/hello.go 2>/dev/null
    echo 'package main\n' > $HOME/go/src/helloworld/hello.go
    echo 'import "fmt"\n' >> $HOME/go/src/helloworld/hello.go
    echo 'func main() { fmt.Println("Hello, world!") }' >> $HOME/go/src/helloworld/hello.go
    go run $HOME/go/src/helloworld/hello.go 1>/dev/null 2>&1
    if [ $? -eq 0 ]; then
        echo "[*] Go file compiled and excecuted correctly"
        echo "[*] Happy coding Gopher!"
        sleep 2
    else
        echo "[!] Go encounterd errors during execution"
        echo "[!] Manual troubleshooting is required"
        sleep 2
    fi
fi

A quick and dirty shell script that will install a golang environment for you. Feel free to also modify it if you find a problem :)

EDIT: I never even noticed how bad the formatting was when I originally posted this lol sorry about that.
« Last Edit: March 30, 2015, 12:34:10 am by Spacecow »

Offline srirachasauce

  • /dev/null
  • *
  • Posts: 17
  • Cookies: -2
    • View Profile
Re: Setting up you Golang Environment
« Reply #2 on: March 27, 2015, 06:34:16 am »
Me gusta. I'd like to see more Golang stuff on this forum :D