Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vsxen
Created April 26, 2018 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsxen/7cc1fae2249c82f24030988cd3232fd7 to your computer and use it in GitHub Desktop.
Save vsxen/7cc1fae2249c82f24030988cd3232fd7 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
"runtime"
"time"
)
// break default func interface select
// case defer go map struct
// chan else goto package switch
// const fallthrough if range type
// continue for import return var
// true false iota nil
// int int8 int16 int32 int64
// uint uint8 uint16 uint32 uint64 uintptr
// float32 float64 complex128 complex64
// bool byte rune string error
// make len cap new append copy close delete
// complex real imag
// panic recover
// * / % <<
// + - | ^
// == != < <=
// >> &
// > >=
// &^
// &&
// ||
// A test
const (
A = iota
B
C
)
var array = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
var stringdemo = []string{"apple", "banan"}
var globalint = 100
//base test
func base() {
var a, b = 2, 4
var c int
d := 5
e := &d
var bool = true
tr := false
fmt.Println("-----------------------------------")
fmt.Println("int", a, b, c, d, *e)
fmt.Println("-----------------------------------")
fmt.Println("boolen", bool, tr)
fmt.Println("-----------------------------------")
fmt.Println("iota & const", A, B, C)
fmt.Println("-----------------------------------")
var arr [10]int
i := 1
for i < 10 {
arr[i] = i
i++
}
fmt.Println("int array", arr)
fmt.Println("byte array", array)
var aSlice []byte
aSlice = array[:3]
fmt.Println("-----------------------------------")
fmt.Println("slice", aSlice)
var num map[string]int
num = make(map[string]int)
num["one"] = 1
num["two"] = 2
fmt.Println("-----------------------------------")
fmt.Println("get value with key in map", num["one"])
fmt.Println(num)
delete(num, "one")
fmt.Println("after delete", num)
}
func flow() {
sum := 0
i := 0
for i < 10 {
sum += i
i++
}
fmt.Println("-----------------------------------")
fmt.Println(sum)
fmt.Println("-----------------------------------")
today := time.Now().Hour()
if today < 12 {
fmt.Println("mornning")
} else {
fmt.Println("afternoon")
}
fmt.Println("now is ", today, "clock")
fmt.Println("-----------------------------------")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X")
case "linux":
fmt.Println("Linux")
default:
fmt.Println("unknown")
}
}
// func name(parameter-list) (result-list) {
// body
// }
func add(x, y int) int {
return x + y
}
func swap(x, y string) (string, string) {
return y, x
}
func split(sum int) (x, y int) {
x = x * x
y = x * 2
return
}
type rect struct {
width, height float64
}
type circ struct {
radius float64
}
func hardarea(r rect) float64 {
return r.width * r.height
}
//func (r ReceiverType) funcName(parameters) results
func (r rect) area() float64 {
return r.height * r.width
}
func (c circ) area() float64 {
return c.radius * c.radius * math.Pi
}
type person struct {
name string
age int
}
type student struct {
person
school string
}
func demostruct() {
var p1 person
p1.name = "xiaoming"
p1.age = 28
fmt.Println("-----------------------------------")
s1 := student{person{"xiaozhang", 15}, "haha"}
fmt.Println("xiaozhang is ", s1.age, "yearsold")
}
func (p person) sayhi() {
fmt.Printf("hi I am %s and %s years\n", p.name, string(p.age))
}
func (p person) sing(lyr string) {
fmt.Printf("la la la %s ... ", lyr)
}
func (s student) sayhi() {
fmt.Printf("hi I am %s and %s years", s.name, string(s.age))
}
type personsing interface {
sayhi()
sing(lyr string)
}
func main() {
base()
flow()
demostruct()
a, b := swap("one", "two")
fmt.Println(add(4, 5), a, b)
fmt.Println(split(5))
r1 := rect{3, 4}
fmt.Println(r1.area())
tom := student{person{"tom", 19}, "tuna"}
var i personsing
i = tom
i.sayhi()
i.sing("------------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment