Notes of Go

Go 使用過程中的記錄

嘗試Grumpy

Google出了一個python到Go的翻譯工具項目。

git clone https://github.com/google/grumpy.git

到本地之後直接運行make失敗,報錯是 big.Int 沒有 Text。原因是Go的版本太低了。於是卸載了1.5版本安裝了1.7版之後make成功。
如何更新Go的版本:https://golang.org/doc/install

一些功能的實現方式

1. 獲取命令行參數

os.Args

2. 文件按行讀取example

golang.org 示例 bufio.Scanner

    file, err := os.Open(filename)
    if err != nil {
        log.Fatal("Open file %s error: %s", filename, err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text()) // output current line
    }

3. 按行讀取json字符串並解析

    file, err := os.Open(filename)
    if err != nil {
        log.Fatal("Open file %s error: %s", filename, err)
    }
    defer file.Close()
    reader := bufio.NewReader(file)
    decoder := json.NewDecoder(reader)
    var m Message // struct for json
    for decoder.More() {
        err := decoder.Decode(&m)
        if err != nil {
            log.Fatal("Error decoding json", err)
        }
    }

4. 本地離線文檔

Go的安裝目錄下包含了完整的doc,只需要運行

godoc -http :8000

就可以在瀏覽器訪問 localhost:8000 或 127.0.0.1:8000 了。
其他用法可以運行godoc查看幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章