Sinisterly
Golang Tutorial #1 - structure, loops, functions, and running code - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: Golang Tutorial #1 - structure, loops, functions, and running code (/Thread-Golang-Tutorial-1-structure-loops-functions-and-running-code)



Golang Tutorial #1 - structure, loops, functions, and running code - Inori - 04-19-2016

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.


RE: Golang Tutorial #1 - structure, loops, functions, and running code - XltSystem - 04-19-2016

Thanks for this tutorial Smile, This seems like an interesting language and without this i would never have come across it xD


RE: Golang Tutorial #1 - structure, loops, functions, and running code - lux - 04-19-2016

Not a bad tutorial, but someStringArray isn't an array, it's a slice

Also you should really cover function signatures and how go declares parameters and return types


RE: Golang Tutorial #1 - structure, loops, functions, and running code - Inori - 04-19-2016

(04-19-2016, 09:43 PM)lux Wrote: Not a bad tutorial, but someStringArray isn't an array, it's a slice

Also you should really cover function signatures and how go declares parameters and return types

Ah, dammit, I always mess up arrays and slices. Too used to other languages. As for signatures and the rest, is that relevant to a basic tutorial?


RE: Golang Tutorial #1 - structure, loops, functions, and running code - lux - 04-19-2016

(04-19-2016, 10:34 PM)Inori Wrote: Ah, dammit, I always mess up arrays and slices. Too used to other languages. As for signatures and the rest, is that relevant to a basic tutorial?

I'd say functions are pretty basic, people should know how Go handles returning IMO


RE: Golang Tutorial #1 - structure, loops, functions, and running code - Inori - 04-20-2016

(04-19-2016, 11:45 PM)lux Wrote: I'd say functions are pretty basic, people should know how Go handles returning IMO

Meh, fair enough. I'll handle (ha) that in a tut or two


RE: Golang Tutorial #1 - structure, loops, functions, and running code - lux - 04-20-2016

(04-20-2016, 02:44 AM)Inori Wrote: Meh, fair enough. I'll handle (ha) that in a tut or two

If I find some time from work I might write up a basic tutorial on goroutines and channels, and basic concurrency within Go

Also it's not a dynamic variable definition, it infers the type at compile - you can't later on change the type via :=


RE: Golang Tutorial #1 - structure, loops, functions, and running code - Inori - 04-20-2016

(04-20-2016, 07:53 AM)lux Wrote: Also it's not a dynamic variable definition, it infers the type at compile - you can't later on change the type via :=

I use the term dynamic loosely. It's not a dynamic variable, but the act of defining it with := is more flexible


RE: Golang Tutorial #1 - structure, loops, functions, and running code - Wildfire - 04-20-2016

I've seen Go around but I've never actually bothered to use it... :/ thanks for the share, I'll take a peek.


RE: Golang Tutorial #1 - structure, loops, functions, and running code - lux - 04-20-2016

(04-20-2016, 12:30 PM)Inori Wrote: I use the term dynamic loosely. It's not a dynamic variable, but the act of defining it with := is more flexible

Yeah but with a static language such as golang, you can't use the word directly Tongue