Skip to content

Instantly share code, notes, and snippets.

@rafops
Created December 2, 2015 02:57
Show Gist options
  • Save rafops/72236075f2746179cad6 to your computer and use it in GitHub Desktop.
Save rafops/72236075f2746179cad6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"runtime"
)
const Pi = 3.1415
func add(x int, y int) int {
return x + y
}
func multiple_returns(x int) (d, t int) {
d = x * 2
t = x * 3
return
}
func variable_declaration() {
var a int
var b string
fmt.Println(a, b)
var c, d int = 3, 4
fmt.Println(c, d)
/* implicit var, only inside a function */
f := 123
fmt.Println(f)
/* some types */
var ui8 uint8 = 255; /* byte */
var xx uintptr = 18446744073709551615
var fff float64 = 3.14159893274928376
fmt.Println(ui8)
fmt.Println(xx)
fmt.Println(fff)
/* type inference */
var s1 string = "ok"
s2 := s1
s1 = "hey"
fmt.Printf("%s, %s\n", s1, s2)
}
func loops() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
/* while */
for sum < 100 {
sum+=sum
}
fmt.Println(sum)
/* infloop */
sq := float64(sum)
for {
sq = math.Sqrt(sq)
if sq < 10 {
break
}
}
fmt.Println(sq)
}
func not_a_number() {
fmt.Println(math.Sqrt(-4)) /* NaN */
}
func if_short_statement() {
/* if short statement */
xxx := 2
if yyy := xxx; yyy < 3 {
fmt.Println("ok!")
}
// fmt.Println(yyy) // no yyy declared
}
func which_os() {
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)
}
}
func pointers() {
i := 42
p := &i
fmt.Println(*p)
*p = 12345
fmt.Println(i)
}
type Vec2 struct {
x int
y int
}
func vec_struct_and_pointer() {
vv := Vec2{2, 3}
fmt.Println(vv)
fmt.Printf("x=%d, y=%d\n", vv.x, vv.y)
p := &vv
p.x = 7
p.y = 9
fmt.Printf("x=%d, y=%d\n", vv.x, vv.y)
}
func arrays() {
var a [2]string
a[0] = "hello"
a[1] = "world"
fmt.Println(a)
b := []int{2, 3, 4, 5}
for i := 0; i < len(b); i++ {
fmt.Printf("- %d\n", b[i])
}
m := [][]int {
[]int{1, 2, 3},
[]int{4, 5, 6},
[]int{7, 8, 9},
}
for i := 0; i < len(m); i++ {
for j := 0; j < len(m[i]); j++ {
fmt.Printf("[%d]", m[i][j])
}
fmt.Printf("\n")
}
xx := []int{2, 3, 4, 5, 6, 7, 8}
ss := xx[3:]
fmt.Println(ss)
ss = append(ss, 2, 3, 4)
fmt.Println(ss)
for i, v := range ss {
fmt.Printf("%d -> %d\n", i, v)
}
for _, v := range ss { // skip index
fmt.Println(v)
}
}
func maps() {
var players map[string]Vec2
players = make(map[string]Vec2)
players["player1"] = Vec2{4, 7}
players["player2"] = Vec2{6, 1}
for k, v := range players {
fmt.Printf("%s => [%d, %d]\n", k, v.x, v.y)
}
m := make(map[string]int)
m["a"] = 1
m["b"] = 2
m["c"] = 3
delete(m, "b")
for _, v := range m {
fmt.Println(v)
}
elem, ok := m["c"]
if ok {
fmt.Println(elem)
}
elem, ok = m["b"]
if !ok {
fmt.Println("b is not set")
}
}
func (vec2 *Vec2) Print() {
fmt.Printf("VEC2: x=%d, y=%d\n", vec2.x, vec2.y)
}
func (vec2 *Vec2) Sprint() string {
return fmt.Sprintf("VEC2: x=%d, y=%d\n", vec2.x, vec2.y)
}
func methods() {
v1 := Vec2{2, 3}
v1.Print()
str := v1.Sprint()
fmt.Println(str)
}
func main() {
fmt.Printf("hello, vlsm, %d\n", add(2, 3))
fmt.Println(multiple_returns(5))
variable_declaration()
loops()
not_a_number()
if_short_statement()
which_os()
pointers()
vec_struct_and_pointer()
arrays()
maps()
methods()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment