02. 一个例子熟悉Go

2019-05-29  本文已影响0人  大鱼人Echo

Go工具/命令

build: compile packages and dependencies

install: compile and install packages and dependencies

get: download and install packages and dependencies

fmt: gofmt(reformat) package sources

test: test packages

## 目录结构
├── main.go
└── main_test.go
package main

import (
    "io"
    "net/http"
)

// Print1to20 答应1到20的和
func Print1to20() int {
    res := 0
    for i := 1; i <= 20; i++ {
        res += i
    }
    return res
}

func firstPage(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "<h1>Hello, this is my first page!</h1>")
}

func main() {
    http.HandleFunc("/", firstPage)
    http.ListenAndServe(":8000", nil)
}

package main

import (
    "fmt"
    "testing"
)

func TestPrint(t *testing.T) {
    res := Print1to20()
    fmt.Println("hey")
    if res != 210 {
        t.Errorf("Wrong result of Print1to20")
    }
}
go test -v
上一篇 下一篇

猜你喜欢

热点阅读