Login Register


Golang Tutorial #1 - structure, loops, functions, and running code filter_list
Author
Message
Golang Tutorial #1 - structure, loops, functions, and running code #1
Go - a language made by google in 2007, in my opinion, is the most readable and most fun widespread static language currently available. While it has some restrictions and some omitted functionality (for the purpose of keeping it very lightweight), it's easy, fun, and intuitive.

Without further adieu, let's get into the tutorial.

Variables and Types

I'm doing variables before functions and structure, because the naming conventions in Go are somewhat lisp-ish, so it's good to learn them first.

Variables, in their regular static form, are defined as follows
Code:
// single variable var someInt int // array, in Go known as a slice var someStringSlice []string

As you can see, the static defining of variables is almost reversed from languages like java which use name, then type. This is also in effect for function arguments and returns.

Dynamic definitions are much easier and straightforward, as they (like python variables) are assigned the type of variables on the right side of the ":=" token.
Code:
someInt:=10 // even when using dynamic definition, you MUST specify the slice type someStringSlice:=[]string{"foo","bar"}

File Structure

File structure in Go is very simple, organized, and important when using packages (which we'll talk about in a later tutorial). Below is a basic example. As a sidenote, semicolons in Go work the same way as they do in Ruby - they're optional 99% of the time, but sometimes (when refactoring, for example) they're needed.

Code:
// package declaration - for single files you want to compile, use the "main" package package main // imports - importing desired packages (you can also use import "package") import( // printing "fmt" // string utility "strconv" ) // set any types/global variables here with those keywords // basic function - func 1.1 func add(x,y int) int{ return x+y } // named return function - func 1.2 func sub(x,y int) (z int){ z=x-y return } // main function func main(){ // set left-side variables to the right-side value's types a,b:=10,5 // manually set variable to the return of our add() function var c int=add(a,b) // set "a" to the return of our sub() method (since "a" already exists, we don't use ":=") a=sub(b,c) // print "a" fmt.Println(a) }

func 1.1:
this function takes 2 integers (x and y) as arguments, and returns the result of their addition

func 1.2:
this function had the same basis as func 1.1, but it has a named return. Using (z int) is the equivalent of typing "var z int" (which initializes an empty integer variable) as the first line in your function. Since the function is named, we simply use "return" with no arguments at the end of the function.

Loops

Loops in go are easy(ish) to use, compared to other static languages. Below are a few examples. Quick note: Go doesn't have a "while" keyword, as is commonly found in other languages, so all loops are technically "for" loops.
Code:
// endless (until exited) loop for{ // do stuff // use "break" to exit the loop } // conditional endless loop for <condition>{ // do stuff // again, breaking works } // for-each loop for index,value:=range []int{10,20,25}{ // index=0, value=10 // index=1, value=20 // index=2, value=25 } // the classic number for loop for i:=0;i<=10;i++{ // 0, 1, 2, 3... }

Running Code

Instead of a mandatory compile to test your code, go offers the option to run it like a Python or Ruby script. To do this, use the following.
Code:
go run myProgram.go

If you want to compile your program, Go will automatically detect your OS so it builds the right executable.
Code:
go build [file]

If you want to cross compile, set the GOOS environment variable to the appropriate OS.
Spoiler: Windows to Linux
Code:
set GOOS=linux go build

Spoiler: Linux to Windows
Code:
GOOS=windows go build


That's it for the first tutorial. I'll probably post another tomorrow, because I'm swamped with school and other stuff tonight. As an end note, I highly recommend poking around here to see how certain packages and elements of Go work. Next time, we'll be looking over type definitions and associated permissed operations, structs, and package structure.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.

[+] 1 user Likes Inori's post
Reply





Messages In This Thread
Golang Tutorial #1 - structure, loops, functions, and running code - by Inori - 04-19-2016, 08:03 PM



Users browsing this thread: 1 Guest(s)