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. 获取命令行参数
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查看帮助。