Getting Started with Go
theodesp
46.2K views
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
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")
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Suggested playgrounds