Basic Types
-
bool(true/false)
-
string
-
int int8 int16 int32 int64 / and uint equivalences, uintptr
-
byte(alias for uint8)
-
rune(alias for int32, represents a Unicode code point)
-
float32 float64
-
complex64 complex128
Operators
-
:=
is used to set a new variable without declaring it, like thisnew_var := 1
-
<<
is used to ... -
%T
stands for type -
var var_name var_type
declares a variable,var var_name converted_var_type = var_type(var_name)
converts the type of a variable, or more simply,converted_var_name := var_type(var_name)
-
Numeric Constants
package main
import "fmt"
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
func needInt(x int) int { return x10 + 1 }
func needFloat(x float64) float64 {
return x 0.1
}
func main() {
fmt.Println(needInt(Small))
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
}
Functions && Packages
-
A Go program starts at
package main
-
You can import
-
An example
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Intn(10))
}
-
This program is using the packages with import paths
"fmt"
and"math/rand"
-
By convention, the package name is the same as the last element of the import path. For instance, the
"math/rand"
package comprises files that begin with the statementpackage rand
-
We can see packages as generic functions in C, whereas functions are actually functions that takes variables and return values
TO BE CONTINUED...
Comments
comments powered by Disqus