Skip to main content

Go

🐱‍💻 Go Numeric Types (bits) - https://golang.org/ref/spec 🐱‍💻 Go Standard Library (bits) - https://golang.org/pkg/

Print

 // Println, Printf, Print, Sprintf
package main

import "fmt"

func main() {
name := "amy"
age := 18
fmt.Print("hello, ")
fmt.Println("world!")
fmt.Print("amy! \n")
fmt.Print("my name is ", name)
fmt.Println("my age is ", age)

fmt.Printf("my name is %v and my age is %v \n", name, age)
fmt.Printf("my name is %q and my age is %v \n", name, age)
fmt.Printf("age is of type %T \n", age)
fmt.Printf("you scored %f points \n", 7.19)
fmt.Printf("you scored %0.1f points \n", 7.19)

var str = fmt.Sprintf("my name is %v and my age is %v \n", name, age)
fmt.Printf("the saved string is: %v", str)
}

array

 // array slice & append
package main

import "fmt"

func main() {
names := []string{"amy", "Shieh", "aliveAmy"}
fmt.Println(names, len(names))
names = append(names, "changqing")
fmt.Println(names, len(names))

startNames := names[:3]
fmt.Println(names, len(names), startNames, len(startNames))

endNames := names[1:]
fmt.Println(endNames)

middleNames := names[0:2]
fmt.Println(middleNames)
}

sort & strings lib


package main

import (
"fmt"
"sort"
"strings"
)

func main() {
greeting := "hello there~"
//Contains
fmt.Println(strings.Contains(greeting, "he"))
//ReplaceAll
fmt.Println(strings.ReplaceAll(greeting, "hello", "Hi!"))
//ToUpper
fmt.Println(strings.ToUpper("amy"))
//Index
fmt.Println(strings.Index("aliveAmy", "a"))
//Split
fmt.Println(strings.Split("aliveAmy", "a"))

ages := []int{7,19,3,4,5,6}
//Sort.Int
sort.Ints(ages)
fmt.Println(ages)
//Sort.SearchInts
fmt.Println(sort.SearchInts(ages, 19))

names := []string{"xie", "chang", "qing", "alive", "amy"}
//Sort.Strings
sort.Strings(names)
fmt.Println(names)
//Sort.SearchStrings
fmt.Println(sort.SearchStrings(names, "amy"))
}

for 循环【index,value】

names := []string{"xie", "chang", "qing", "alive", "amy", "aliveAmy"}

while

i := 0
for i < 7 {
fmt.Printf("Value of x is %v", i);
i++
}

for


for i := 0; i < 7; i++ {
fmt.Printf("i is %v", i)
}

range (for of)


for index, value := range names {
fmt.Printf("the index %v of name %v is", index, name)
}

for _, value := range names {
fmt.Printf("the name %v is", name)
}

note
for _, value := range name {
value = "new name"
}

fmt.Println(names) //[xie chang qing alive amy aliveAmy]

It does not change the original value

condition for loop


names := []string{"xie", "chang", "qing", "alive", "amy", "aliveAmy"}

for index, name := range names {
if index == 0 {
fmt.Println("ignore it")
continue
}

if index == (len(names) - 1) {
fmt.Printf("%v is the last one\n", name)
break
}

fmt.Printf("the pos %v of name %v is \n", index, name)

}

function call

package main
import (
"fmt"
"math"
)

func greeting(p string) {
fmt.Printf("Good morning %v ! \n", p)
}

func greeting2EveryOne(names []string, f func(name string)) {
for _, name := range names {
f(name)
}
}

func calculateCircleArea(r float64) float64 {
return math.Pi * r * r
}


func main() {
names := []string{"xie", "chang", "qing", "alive", "amy", "aliveAmy"}
greeting( "amy")
greeting2EveryOne(names, greeting)
circle1 := calculateCircleArea(2.5)
circle2 := calculateCircleArea(5)

fmt.Printf("circle1's area is %0.1f, circle2's area is %0.3f", circle1, circle2)
}

multiple value return

package main

import (
"fmt"
"strings"
)

//go中函数返回多个值就是这样的写法,那假如参数很多的时候,有没有更好地写法呢?
func getMultipleName(n string) (string, string) {
names := strings.Split(n, " ")
if len(names) > 1 {
// 感觉有点像js中数组、对象解构赋值的写法呢~
return names[0], names[1]
}
return names[0], "_"
}

func main() {
firstName, lastName := getMultipleName("alive Amy")
firstName1, lastName1 := getMultipleName("alive")
fmt.Println(firstName, lastName)
fmt.Println(firstName1, lastName1)
}

package ref

parent.go
package main
import "fmt"
var hobbies = []string{"reading", "painting"}
func main() {
sayHello("amy")
for _, value := range getNamesFromChild {
fmt.Println(value)
}
getValueFromParent()
}

child.go
package main
import "fmt"

var getNamesFromChild = []string{"xie", "chang", "qing", "alive", "amy", "aliveAmy"}

func sayHello(name string) {
fmt.Printf("hello, %v", name)
}
func getValueFromParent() {
for _, value := range hobbies{
fmt.Println(value)
}
}

package call shell


go run parent.go child.go

Map【key, value】

package main
import "fmt"

func main() {
names := map[int]string {
0: "amy",
1: "aliveAmy",
2: "changqing",
3: "amyShieh",
}

for key, value := range names {
fmt.Println(key, "-", value)
}
}

passing value

"引用类型"的存储:堆栈, address => stack, value => heap reference "基本类型"的存储: stack, copies

basicreference

Strings

Ints

floats

Booleans

Arrary

Slices

Maps

Functions

pointer

package main

import "fmt"

func updateName(n *string) {
*n = "aliveAmy"
}

func main() {
name := "amy"
position := &name
fmt.Println("original name", name)
updateName(position)
fmt.Println("changed name", name)
}

Custom type & struct

bill.go

package main

type bill struct {
name string
items map[string] float64
tips float64
}

func newBill(name string) bill {
myBill := bill{
name: name,
items: map[string]float64 {},
tips: 0,
}
return myBill
}

mian.go

package main

import "fmt"

func main() {
fmt.Println(newBill("amy's bill"))
}

go run main.go bill.go