Go 学习笔记

Go: Helloworld

package main

import "fmt"

func main() {
    fmt.Println("Hi 奥特曼")
}

To run in terminal
go run main.go
or
go build main.go >> main >> ./main (main.exe)

package

package == project == workspace
package main means executable. It's about package not func.
package main >> go build >> main.exe --- executable
package helper >> go build >> nothing --- reusable


Basic

  1. initialize a variable
    var card string = "Ace of Spades"
    or
    card := "Ace of Spades"

  2. function
    func myFunc() string { ... }

  3. slice
    cards := []string{"Ace of Diamonds", newCard()}
    cards = append(cards, "Six of Diamonds")

  4. loop

for index, item := range items {
    fmt.Println(item)
}
  1. if else statement
if number%2 == 0 {
    fmt.Println(number, " is odd")
} else { //same line
    fmt.Println(number, " is even")
}

Go: Type

1. Definition

type deck []string

type person struct {
    lastName  string
    firstName string
    contact   contactInfo //can only have `contactInfo` in this line
}

type contactInfo struct {
    email string
    zip   int
}

2. Receiver func

Any variable of type 'deck' gets access to method print()

func (d deck) print() {
    for i, card := range d {
        fmt.Println(i, card)
    }
}

func (pointerToPerson *person) updateName(newFirstName string) {
    (*pointerToPerson).firstName = newFirstName
}

in convention of go, d in (d deck) will not be replaced by "this" or "self"

3. Pointer

&variable : the memory address of the value this variable is pointing at
*pointer : the value this memory address is pointing at

receiver func can be called by both type and pointer(*type)

4. reference types in Go

  • slices
  • maps
  • channles
  • pointers
  • functions

when a slice is created, Go will automatically create an array and a structure that records the length of the slice, the capacity of the slice and a reference to the underlying array.


Go: Testing

To make a test, new file ending with _test.go
run it,
go test
test method
func TestMyFunc


Go: Interfaces

package main

import "fmt"

type bot interface {
    getGreeting() string
}

type englishBot struct {
}

type spanishBot struct {
}

func main() {
    eb := englishBot{}
    sb := spanishBot{}

    printGreeting(eb)
    printGreeting(sb)
}

func printGreeting(b bot) {
    fmt.Println(b.getGreeting())
}

func (englishBot) getGreeting() string {
    return "Hello"
}

func (spanishBot) getGreeting() string {
    return "Hola"
}

Channels & Go Routines

package main

import (
    "fmt"
    "net/http"
    "time"
)

func main() {
    links := []string{
        "http://google.com",
        "http://golang.org",
        "http://stackoverflow.com",
    }

    c := make(chan string)

    for _, link := range links {
        go checkLink(link, c)
    }

    for l := range c {
        go func(link string) {
            time.Sleep(time.Second * 3)
            checkLink(link, c)
        }(l)
    }
}

func checkLink(link string, c chan string) {
    _, err := http.Get(link)

    if err != nil {
        fmt.Println(link, "might be down!")
        c <- link
        return
    }

    fmt.Println(link, "is up!")
    c <- link
}

你可能感兴趣的:(Go 学习笔记)