Getting Started with Go

theodesp
49.8K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Basics of Go

Variables and Declarations

You can declare a variable of a specific type like this:

var i int

by default that assigns a default value of 0 for this type. For other types for example booleans it is false, for strings is "" and so on.

Go has a shorthand variable declaration operator, :=, which can infer the type:

var i := 1000 // notice => No semicolons!

note that you cannot redeclare the i using := as it has already been declared:

var i := 1000

// Compiler error
i := 1001

you can also use that for multiple declarations and assignments:

name, money := "Theo", 1000
1. Which of the following are valid types in Go?
2 . What is the value of c after this statement?: var c complex128

Functions Declarations

Bellow are some examples of function declarations in Go:

func log(message string) { // takes 1 parameter
}

func add(a int, b int) int { // takes 2 int parameters and returns an int
}

func power(name string) (int, bool) { // takes 1 string parameter and returns an int and a boolean
}

To use just simply call the functions. If you do not care about the return values use _

_, isPowerful = power("Theo")
1. How can I call a function `cdd` that takes 2 doubles and return 3 ints and discard the second?
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content