Created
November 19, 2016 20:34
-
-
Save kodekracker/c46d571f23e6fbd76ddee43ad087a0c9 to your computer and use it in GitHub Desktop.
Learn Go lang (https://golang.org/) in one file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// every go program is package | |
package main | |
// factored import modules | |
import ( | |
"fmt" | |
"time" | |
"runtime" | |
"math" | |
// last name become package name mostly | |
"math/rand" | |
"math/cmplx" | |
) | |
// function with arguments and return value | |
func add(x int, y int) int { | |
return x + y | |
} | |
// multile return values | |
func swap(x, y string) (string, string) { | |
return y, x | |
} | |
// naked return or named return values | |
func split(sum int) (x, y int) { | |
x = sum * 4 / 9 | |
y = sum - x | |
return | |
} | |
// package level variables | |
var c, python, java bool | |
// variables with initializers | |
var i, j int = 1, 2 | |
// factored variables declaration | |
var ( | |
// bool | |
// string | |
// int int8 int16 int32 int64 | |
// uint uint8 uint16 uint32 uint64 uintptr | |
// byte // alias for uint8 | |
// rune // alias for int32 | |
// // represents a Unicode code point | |
// float32 float64 | |
// complex64 complex128 | |
ToBe bool = false | |
MaxInt uint64 = 1<<64 - 1 | |
z complex128 = cmplx.Sqrt(-5 + 12i) | |
) | |
// numeric constants are high precision values | |
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 x*10 + 1 } | |
func needFloat(x float64) float64 { | |
return x * 0.1 | |
} | |
func main() { | |
fmt.Println("Hello World") | |
fmt.Println("The time is", time.Now()) | |
fmt.Println("My favourite number is", rand.Intn(10)) | |
fmt.Printf("Now you have %g problems.\n", math.Sqrt(7)) | |
// only function starts with capital letter can be import/called of module | |
fmt.Println("Pi is ", math.Pi) | |
fmt.Println("Sum of 1 and 2 is ", add(1, 2)) | |
a, b := swap("hello", "world") | |
fmt.Println(a, b) | |
fmt.Println(split(17)) | |
// function level variable | |
var i int | |
fmt.Println(i, c, python, java) | |
// If an initializer is present, the type can be omitted | |
var c, python, java = true, false, "no!" | |
fmt.Println(i, j, c, python, java) | |
var j int = 2 | |
// short variable declaration inside a function only with implicit type | |
k := 3 | |
c1, python1, java1 := true, false, "no!" | |
fmt.Println(i, j, k, c1, python1, java1) | |
// constants cannot be declared using the := syntax | |
const f = "%T(%v)\n" | |
fmt.Printf(f, ToBe, ToBe) | |
fmt.Printf(f, MaxInt, MaxInt) | |
fmt.Printf(f, z, z) | |
// zero values variable declaration | |
var i1 int | |
var f1 float64 | |
var b1 bool | |
var s1 string | |
fmt.Printf("%v %v %v %q\n", i1, f1, b1, s1) | |
// type conversions | |
var x, y int = 3, 4 | |
var f2 float64 = math.Sqrt(float64(x*x + y*y)) | |
var z uint = uint(f2) | |
fmt.Println(x, y, z) | |
// type inference | |
v := 42 // change me! | |
fmt.Printf("v is of type %T\n", v) | |
// numeric constants | |
fmt.Println(needInt(Small)) | |
fmt.Println(needFloat(Small)) | |
fmt.Println(needFloat(Big)) | |
// for loop (only one looping construct in Go lang) | |
sum := 0 | |
for i := 0; i < 10; i++ { | |
sum += i | |
} | |
fmt.Println(sum) | |
// init and post statement are optional | |
sum1 := 1 | |
for ; sum1 < 1000; { | |
sum1 += sum1 | |
} | |
fmt.Println(sum1) | |
// for loop is Go's "while loop" | |
sum2 := 1 | |
for sum2 < 1000 { | |
sum2 += sum2 | |
} | |
fmt.Println(sum2) | |
// infinite for loop | |
// for { | |
// } | |
// if | |
x1 := float64(-4) | |
if x1 < 0 { | |
fmt.Println(fmt.Sprint(math.Sqrt(-x1)) + "i") | |
} | |
fmt.Println(fmt.Sprint(math.Sqrt(2))) | |
// if with a short statement to execute before the condition | |
var x2, n, lim float64 = 3, 2, 10 | |
if v := math.Pow(x2, n); v < lim { | |
fmt.Println(v) | |
} | |
// if and else | |
if v := math.Pow(x2, n); v < lim { | |
fmt.Println(v) | |
} else { | |
fmt.Printf("%g >= %g\n", v, lim) | |
} | |
// can't use v here, though | |
fmt.Println(lim) | |
// switch of Go | |
fmt.Print("Go runs on ") | |
switch os := runtime.GOOS; os { | |
case "darwin": | |
fmt.Println("OS X.") | |
case "linux": | |
fmt.Println("Linux.") | |
default: | |
// freebsd, openbsd, | |
// plan9, windows... | |
fmt.Printf("%s.", os) | |
} | |
// switch with no condition is the same as switch true | |
t := time.Now() | |
switch { | |
case t.Hour() < 12: | |
fmt.Println("Good morning!") | |
case t.Hour() < 17: | |
fmt.Println("Good afternoon.") | |
default: | |
fmt.Println("Good evening.") | |
} | |
// defer keyword defers the execution of a function until the surrounding | |
// function returns | |
defer fmt.Println("world") | |
fmt.Println("hello") | |
// stacking defers | |
// when a function returns, its deferred calls are executed in | |
// last-in-first-out order | |
fmt.Println("counting") | |
for i := 0; i < 10; i++ { | |
defer fmt.Println(i) | |
} | |
fmt.Println("done") | |
// Pointers (holds the memory address of a variable) | |
i2, j2 := 42, 2701 | |
p := &i2 // point to i2 | |
fmt.Println(*p) // read i2 through the pointer | |
*p = 21 // set i2 through the pointer | |
fmt.Println(i2) // see the new value of i2 | |
p = &j // point to j2 | |
*p = *p / 37 // divide j2 through the pointer | |
fmt.Println(j2) // see the new value of j2 | |
// Structs (collection of fields) | |
type Vertex struct { | |
X int | |
Y int | |
// X, Y int (this is also right syntax of declaration) | |
} | |
fmt.Println(Vertex{1, 2}) | |
// Struct fields are accessed using a dot. | |
v1 := Vertex{1, 2} | |
v1.X = 4 | |
fmt.Println(v1.X) | |
// Pointers to Structs | |
p1 := &v1 | |
// or you can access field as (*p).X | |
p1.X = 1e9 | |
fmt.Println(v1) | |
// Struct Literals | |
var ( | |
v2 = Vertex{1, 2} // has type Vertex | |
v3 = Vertex{X: 1} // Y:0 is implicit | |
v4 = Vertex{} // X:0 and Y:0 | |
p2 = &Vertex{1, 2} // has type *Vertex | |
) | |
fmt.Println(v2, p2, v3, v4) | |
// Arrays | |
var a1 [2]string | |
a1[0] = "Hello" | |
a1[1] = "World" | |
fmt.Println(a1[0], a1[1]) | |
fmt.Println(a1) | |
primes := [6]int{2, 3, 5, 7, 11, 13} | |
fmt.Println(primes) | |
// Array Slice | |
var s []int = primes[1:4] | |
fmt.Println(s) | |
// Slices are like references to Arrays | |
names := [4]string{ | |
"John", | |
"Paul", | |
"George", | |
"Ringo", | |
} | |
fmt.Println(names) | |
a3 := names[0:2] | |
b3 := names[1:3] | |
fmt.Println(a3, b3) | |
b3[0] = "XXX" | |
fmt.Println(a3, b3) | |
fmt.Println(names) | |
// Slice Literals | |
q := []int{2, 3, 5, 7, 11, 13} | |
fmt.Println(q) | |
r := []bool{true, false, true, true, false, true} | |
fmt.Println(r) | |
s2 := []struct { | |
i int | |
b bool | |
}{ | |
{2, true}, | |
{3, false}, | |
{5, true}, | |
{7, true}, | |
{11, false}, | |
{13, true}, | |
} | |
fmt.Println(s2) | |
// Slice defaults (omit lower or upper bound takes defaults) | |
s3 := []int{2, 3, 5, 7, 11, 13} | |
s3 = s3[1:4] | |
fmt.Println(s3) | |
s = s3[:2] | |
fmt.Println(s3) | |
s3 = s3[1:] | |
fmt.Println(s3) | |
// Slice length and capacity | |
s4 := []int{2, 3, 5, 7, 11, 13} | |
fmt.Println(s4) | |
// Slice the slice to give it zero length. | |
s4 = s4[:0] | |
fmt.Println(s4) | |
// Extend its length. | |
s4 = s4[:4] | |
fmt.Println(s4) | |
// Drop its first two values. | |
s4 = s4[2:] | |
fmt.Println(s4) | |
// Nil Slice (length=0, capacity=0) | |
var s5 []int | |
fmt.Println(s5, len(s5), cap(s5)) | |
if s5 == nil { | |
fmt.Println("nil!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment