Go ( )

Go ( )

[www.liwenzhou.com/][https://w ]

Golang studygolang.com/pkgdoc

Go ( )

1. fmt

fmt C printf scanf I/O

1.1

fmt

1.1.1 Print

Print
Print
Printf
Println

func Print(a ...interface{}) (n int, err error) func Printf(format string, a ...interface{}) (n int, err error) func Println(a ...interface{}) (n int, err error)

func main() { fmt.Print(" ") name := " " fmt.Printf(" %s\n", name) fmt.Println(" ") }

1.1.2 Fprint

Fprint
io.Writer
w

func Fprint(w io.Writer, a ...interface{}) (n int, err error) func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Fprintln(w io.Writer, a ...interface{}) (n int, err error)

// fmt.Fprintln(os.Stdout, " ") fileObj, err := os.OpenFile("./xx.txt", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { fmt.Println(" err:", err) return } name := " " // fmt.Fprintf(fileObj, " %s", name)

io.Writer

1.1.3 Sprint

Sprint

func Sprint(a ...interface{}) string func Sprintf(format string, a ...interface{}) string func Sprintln(a ...interface{}) string

s1 := fmt.Sprint(" ") name := " " age := 18 s2 := fmt.Sprintf("name:%s,age:%d", name, age) fmt.Println(s2) s3 := fmt.Sprintln(" ") fmt.Println(s1, s2, s3)

1.1.4 Errorf

Errorf
format

func Errorf(format string, a ...interface{}) error

err := fmt.Errorf(" ")

Go1.13

fmt.Errorf
%w
Error Wrapping Error

e := errors.New(" e") w := fmt.Errorf("Wrap %w", e)

1.2

*printf
format

1.2.1

%v
%+v %v
%#v Go
%T
%%

fmt.Printf("%v\n", 100) fmt.Printf("%v\n", false) o := struct{ name string }{" "} fmt.Printf("%v\n", o) fmt.Printf("%#v\n", o) fmt.Printf("%T\n", o) fmt.Printf("100%%\n")

100 false { } struct { name string }{name:" "} struct { name string } 100%

1.2.2

%ttrue false

1.2.3

%b
%c unicode
%d
%o
%x a-f
%X A-F
%U Unicode U+1234 U+%04X
%q go

n := 65 fmt.Printf("%b\n", n) fmt.Printf("%c\n", n) fmt.Printf("%d\n", n) fmt.Printf("%o\n", n) fmt.Printf("%x\n", n) fmt.Printf("%X\n", n)

1000001 A 65 101 41 41

1.2.4

%b -123456p-78
%e -1234.456e+78
%E -1234.456E+78
%f 123.456
%F %f
%g %e %f
%G %E %F

f := 12.34 fmt.Printf("%b\n", f) fmt.Printf("%e\n", f) fmt.Printf("%E\n", f) fmt.Printf("%f\n", f) fmt.Printf("%g\n", f) fmt.Printf("%G\n", f)

6946802425218990p-49 1.234000e+01 1.234000E+01 12.340000 12.34 12.34

1.2.5 []byte

%s []byte
%q go
%x a-f
%X A-F

s := " " fmt.Printf("%s\n", s) fmt.Printf("%q\n", s) fmt.Printf("%x\n", s) fmt.Printf("%X\n", s)

" " e5b08fe78e8be5ad90 E5B08FE78E8BE5AD90

1.2.6

%p 0x

a := 10 fmt.Printf("%p\n", &a) fmt.Printf("%#p\n", &a)

0xc000094000 c000094000

1.2.7

0

%f
%9f 9
%.2f 2
%9.2f 9 2
%9.f 9 0

n := 12.34 fmt.Printf("%f\n", n) fmt.Printf("%9f\n", n) fmt.Printf("%.2f\n", n) fmt.Printf("%9.2f\n", n) fmt.Printf("%9.f\n", n)

12.340000 12.340000 12.34 12.34 12

1.2.8 falg

+ %q %+q ASCII
%x %X % x % X
-
# 0 %#o 0x %#x 0X %#X 0x %#p %q %#q %U %#U go
0 0 0

s := " " fmt.Printf("%s\n", s) fmt.Printf("%5s\n", s) fmt.Printf("%-5s\n", s) fmt.Printf("%5.7s\n", s) fmt.Printf("%-5.7s\n", s) fmt.Printf("%5.2s\n", s) fmt.Printf("%05s\n", s)

00

1.3

Go

fmt
fmt.Scan
fmt.Scanf
fmt.Scanln

1.3.1 fmt.Scan

func Scan(a ...interface{}) (n int, err error)
  • Scan

func main() { var ( name string age int married bool ) fmt.Scan(&name, &age, &married) fmt.Printf(" name:%s age:%d married:%t/n", name, age, married) }

28
false

$ ./scan_demo 28 false name: age:28 married:false

fmt.Scan

1.3.2 fmt.Scanf

func Scanf(format string, a ...interface{}) (n int, err error)
  • Scanf format

func main() { var ( name string age int married bool ) fmt.Scanf("1:%s 2:%d 3:%t", &name, &age, &married) fmt.Printf(" name:%s age:%d married:%t/n", name, age, married) }

28
false

$ ./scan_demo 1: 2:28 3:false name: age:28 married:false

fmt.Scanf
fmt.Scan
fmt.Scanf

fmt.Scanf

$ ./scan_demo 28 false name: age:0 married:false

1.3.3 fmt.Scanln

func Scanln(a ...interface{}) (n int, err error)
  • Scanln Scan

func main() { var ( name string age int married bool ) fmt.Scanln(&name, &age, &married) fmt.Printf(" name:%s age:%d married:%t/n", name, age, married) }

28
false

$ ./scan_demo 28 false name: age:28 married:false

fmt.Scanln

1.3.4 bufio.NewReader

bufio

func bufioDemo() { reader := bufio.NewReader(os.Stdin) // fmt.Print(" ") text, _ := reader.ReadString('\n') // text = strings.TrimSpace(text) fmt.Printf("%#v\n", text) }

1.3.5 Fscan

fmt.Scan
fmt.Scanf
fmt.Scanln
io.Reader

func Fscan(r io.Reader, a ...interface{}) (n int, err error) func Fscanln(r io.Reader, a ...interface{}) (n int, err error) func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error)

1.3.6 Sscan

fmt.Scan
fmt.Scanf
fmt.Scanln

func Sscan(str string, a ...interface{}) (n int, err error) func Sscanln(str string, a ...interface{}) (n int, err error) func Sscanf(str string, format string, a ...interface{}) (n int, err error)

2. time

time

2.1

time.Time
time.Now()

func timeDemo() { now := time.Now() // fmt.Printf("current time:%v\n", now) year := now.Year() // month := now.Month() // day := now.Day() // hour := now.Hour() // minute := now.Minute() // second := now.Second() // fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) }

2.2

1970 1 1 08:00:00GMT Unix UnixTimestamp

func timestampDemo() { now := time.Now() // timestamp1 := now.Unix() // timestamp2 := now.UnixNano() // fmt.Printf("current timestamp1:%v\n", timestamp1) fmt.Printf("current timestamp2:%v\n", timestamp2) }

time.Unix()

func timestampDemo2(timestamp int64) { timeObj := time.Unix(timestamp, 0) // fmt.Println(timeObj) year := timeObj.Year() // month := timeObj.Month() // day := timeObj.Day() // hour := timeObj.Hour() // minute := timeObj.Minute() // second := timeObj.Second() // fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) }

2.3

time.Duration
time
time.Duration
290

time

const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond 2. = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute )

time.Duration
1
time.Second
1

2.4

2.4.1 Add

+ Go Add

func (t Time) Add(d Duration) Time

func main() { now := time.Now() later := now.Add(time.Hour) // 1 fmt.Println(later) }

2.4.2 Sub

func (t Time) Sub(u Time) Duration

t-u Duration//t-d d Duration t.Add(-d)

2.4.3 Equal

func (t Time) Equal(u Time) bool

t==u

2.4.4 Before

func (t Time) Before(u Time) bool

t u

2.4.5 After

func (t Time) After(u Time) bool

t u

2.5

time.Tick( )
channel

func tickDemo() { ticker := time.Tick(time.Second) //1 for i := range ticker { fmt.Println(i)// } }

2.6

Format
Go
Y-m-d H:M:S
Go 2006 1 2 15 04 2006 1 2 3 4

12

PM

func formatDemo() { now := time.Now() // Go 2006 1 2 15 04 Mon Jan //24 fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) //12 fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan")) fmt.Println(now.Format("2006/01/02 15:04")) fmt.Println(now.Format("15:04 2006/01/02")) fmt.Println(now.Format("2006/01/02")) }

2.6.1

now := time.Now() fmt.Println(now) // loc, err := time.LoadLocation("Asia/Shanghai") if err != nil { fmt.Println(err) return } // timeObj, err := time.ParseInLocation("2006/01/02 15:04:05", "2019/08/04 14:15:20", loc) if err != nil { fmt.Println(err) return } fmt.Println(timeObj) fmt.Println(timeObj.Sub(now))

3. Go

3.1

3.1.1

Go

string
Go
type

struct

//MyInt int type MyInt int

type
MyInt
int

3.1.2

Go1.9

TypeAlias Type TypeAlias Type

type TypeAlias = Type

rune
byte

type byte = uint8 type rune = int32

3.1.3

// type NewInt int // type MyInt = int func main() { var a NewInt var b MyInt fmt.Printf("type of a:%T\n", a) //type of a:main.NewInt fmt.Printf("type of b:%T\n", b) //type of b:int }

a

main.NewInt
main
NewInt
b
int
MyInt
MyInt

3.2

Go Go

struct
struct

Go

struct

3.2.1

type
struct

type struct { }

Person

type person struct { name string city string age int8 }

type person1 struct { name, city string age int8 }

person
name
city
age
person

3.2.2

var

var

3.2.3

type person struct { name string city string age int8 } func main() { var p1 person p1.name = " " p1.city = " " p1.age = 18 fmt.Printf("p1=%v\n", p1) //p1={ 18} fmt.Printf("p1=%#v\n", p1) //p1=main.person{name:" ", city:" ", age:18} }

.
,
p1.name
p1.age

3.2.4

package main import ( "fmt" ) func main() { var user struct{Name string; Age int} user.Name = " " user.Age = 18 fmt.Printf("%#v\n", user) }

3.2.5

new

var p2 = new(person) fmt.Printf("%T\n", p2) //*main.person fmt.Printf("p2=%#v\n", p2) //p2=&main.person{name:"", city:"", age:0}

p2

Go

.

var p2 = new(person) p2.name = " " p2.age = 28 p2.city = " " fmt.Printf("p2=%#v\n", p2) //p2=&main.person{name:" ", city:" ", age:28}

3.2.6

&
new

p3 := &person{} fmt.Printf("%T\n", p3) //*main.person fmt.Printf("p3=%#v\n", p3) //p3=&main.person{name:"", city:"", age:0} p3.name = " " p3.age = 30 p3.city = " " fmt.Printf("p3=%#v\n", p3) //p3=&main.person{name:" ", city:" ", age:30}

p3.name = " "
(*p3).name = " "
Go

3.2.7

type person struct { name string city string age int8 } func main() { var p4 person fmt.Printf("p4=%#v\n", p4) //p4=main.person{name:"", city:"", age:0} }

3.2.8

p5 := person{ name: " ", city: " ", age: 18, } fmt.Printf("p5=%#v\n", p5) //p5=main.person{name:" ", city:" ", age:18}

p6 := &person{ name: " ", city: " ", age: 18, } fmt.Printf("p6=%#v\n", p6) //p6=&main.person{name:" ", city:" ", age:18}

p7 := &person{ city: " ", } fmt.Printf("p7=%#v\n", p7) //p7=&main.person{name:"", city:" ", age:0}

3.2.9

p8 := &person{ " ", " ", 28, } fmt.Printf("p8=%#v\n", p8) //p8=&main.person{name:" ", city:" ", age:28}

3.3

type test struct { a int8 b int8 c int8 d int8 } n := test{ 1, 2, 3, 4, } fmt.Printf("n.a %p\n", &n.a) fmt.Printf("n.b %p\n", &n.b) fmt.Printf("n.c %p\n", &n.c) fmt.Printf("n.d %p\n", &n.d)

n.a 0xc0000a0060 n.b 0xc0000a0061 n.c 0xc0000a0062 n.d 0xc0000a0063

Go : Go

3.3.1

var v struct{} fmt.Println(unsafe.Sizeof(v)) //0

3.4

Go

person
struct

func newPerson(name, city string, age int8) *person { return &person{ name: name, city: city, age: age, } }

p9 := newPerson(" ", " ", 90) fmt.Printf("%#v\n", p9) //&main.person{name:" ", city:" ", age:90}

3.5

Go

Method
Receiver
this
self

func ( ) ( ) ( ) { }

  • self
    this
    Person
    p
    Connector
    c

//Person type Person struct { name string age int8 } //NewPerson func NewPerson(name string, age int8) *Person { return &Person{ name: name, age: age, } } //Dream Person func (p Person) Dream() { fmt.Printf("%s Go/n", p.name) } func main() { p1 := NewPerson(" ", 25) p1.Dream() }

3.5.1

this
self
Person
SetAge

//SetAge p // func (p *Person) SetAge(newAge int8) { p.age = newAge }

func main() { p1 := NewPerson(" ", 25) fmt.Println(p1.age) //25 p1.SetAge(30) fmt.Println(p1.age) //30 }

3.5.2

Go

//SetAge2 p // func (p Person) SetAge2(newAge int8) { p.age = newAge } func main() { p1 := NewPerson(" ", 25) p1.Dream() fmt.Println(p1.age) //25 p1.SetAge2(30) //(*p1).SetAge2(30) fmt.Println(p1.age) //25 }

3.5.3

3.6

Go

int
type

//MyInt int MyInt type MyInt int //SayHello MyInt SayHello func (m MyInt) SayHello() { fmt.Println("Hello, int ") } func main() { var m1 MyInt m1.SayHello() //Hello, int m1 = 100 fmt.Printf("%#v %T\n", m1, m1) //100 main.MyInt }

3.7

//Person Person type Person struct { string int } func main() { p1 := Person{ " ", 18, } fmt.Printf("%#v\n", p1) //main.Person{string:" ", int:18} fmt.Println(p1.string, p1.int) // 18 }

** **

3.8

//Address type Address struct { Province string City string } //User type User struct { Name string Gender string Address Address } func main() { user1 := User{ Name: " ", Gender: " ", Address: Address{ Province: " ", City: " ", }, } fmt.Printf("user1=%#v\n", user1)//user1=main.User{Name:" ", Gender:" ", Address:main.Address{Province:" ", City:" "}} }

3.8.1

user

Address

//Address type Address struct { Province string City string } //User type User struct { Name string Gender string Address // } func main() { var user2 User user2.Name = " " user2.Gender = " " user2.Address.Province = " " // user2.City = " " // fmt.Printf("user2=%#v\n", user2) //user2=main.User{Name:" ", Gender:" ", Address:main.Address{Province:" ", City:" "}} }

3.8.2

//Address type Address struct { Province string City string CreateTime string } //Email type Email struct { Account string CreateTime string } //User type User struct { Name string Gender string Address Email } func main() { var user3 User user3.Name = " " user3.Gender = " " //user3.CreateTime = "2019"//ambiguous selector user3.CreateTime user3.Address.CreateTime = "2000" //Address CreateTime user3.Email.CreateTime = "2000" //Email CreateTime }

3.9

Go

//Animal type Animal struct { name string } func (a *Animal) move() { fmt.Printf("%s/n", a.name) } //Dog type Dog struct { Feet int8 *Animal // } func (d *Dog) wang() { fmt.Printf("%s ~\n", d.name) } func main() { d1 := &Dog{ Feet: 4, Animal: &Animal{ // name: " ", }, } d1.wang() //~ d1.move() // }

3.10

3.11 JSON

JSON(JavaScript Object Notation) JSON JS/

""
:
,

//Student type Student struct { ID int Gender string Name string } //Class type Class struct { Title string Students []*Student } func main() { c := &Class{ Title: "101", Students: make([]*Student, 0, 200), } for i := 0; i < 10; i++ { stu := &Student{ Name: fmt.Sprintf("stu%02d", i), Gender: " ", ID: i, } c.Students = append(c.Students, stu) } //JSON -->JSON data, err := json.Marshal(c) if err != nil { fmt.Println("json marshal failed") return } fmt.Printf("json:%s\n", data) //JSON JSON --> str := `{"Title":"101","Students":[{"ID":0,"Gender":" ","Name":"stu00"},{"ID":1,"Gender":" ","Name":"stu01"},{"ID":2,"Gender":" ","Name":"stu02"},{"ID":3,"Gender":" ","Name":"stu03"},{"ID":4,"Gender":" ","Name":"stu04"},{"ID":5,"Gender":" ","Name":"stu05"},{"ID":6,"Gender":" ","Name":"stu06"},{"ID":7,"Gender":" ","Name":"stu07"},{"ID":8,"Gender":" ","Name":"stu08"},{"ID":9,"Gender":" ","Name":"stu09"}]}` c1 := &Class{} err = json.Unmarshal([]byte(str), c1) if err != nil { fmt.Println("json unmarshal failed!") return } fmt.Printf("%#v\n", c1) }

3.12 Tag

Tag
Tag

`key1:"value1" key2:"value2"`

tag tag

Tag
key value

Student
json Tag

//Student type Student struct { ID int `json:"id"` //tag json key Gender string //json key name string //json } func main() { s1 := Student{ ID: 1, Gender: " ", name: " ", } data, err := json.Marshal(s1) if err != nil { fmt.Println("json marshal failed!") return } fmt.Printf("json str:%s\n", data) //json str:{"id":1,"Gender":" "} }

3.13

slice map

type Person struct { name string age int8 dreams []string } func (p *Person) SetDreams(dreams []string) { p.dreams = dreams } func main() { p1 := Person{name: " ", age: 18} data := []string{" ", " ", " "} p1.SetDreams(data) // p1.dreams data[1] = " " fmt.Println(p1.dreams) //? }

slice

func (p *Person) SetDreams(dreams []string) { p.dreams = make([]string, len(dreams)) copy(p.dreams, dreams) }

slice map