函数接口
bufio.NewReader将会返回一个执行Reader对象的指针
从键盘输入
| import ( "fmt" "os" "bufio" ) func main(){ var newreader *bufio.Reader = bufio.NewReader(os.Stdin) input,err := newreader.ReadString('\n') if(err==nil){ fmt.Printf("you input string is %s",input) } }
|
文件io
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import ( "fmt" "os" "bufio" ) func main(){ inputfile,inputerr := os.Open("file.txt") if(input != nil){ fmt.Println("open file error!") } defer inputfile.Close() newfilereader := bufio.NewReader(inputfile) inputstring,inputerr := newfilereader.ReadString('\n') if(inputerr != nil){ fmt.Println("read file error!") } fmt.Printf("input string is :%s",inputstring) }
|
或者
| import ( "fmt" "os" "io/ioutil" "bufio" ) func main(){ }
|
go 包管理
Go 1.5 引入了vendor 机制,但是需要手动设置环境变量 GO15VENDOREXPERIMENT= 1,Go编译器才能启用。
从Go1.6起,默认开启 vendor 目录查找,vendor 机制就是在包中引入 vendor 目录,将依赖的外部包复制到 vendor 目录下,编译器在查找外部依赖包时,优先在 vendor 目录下查找。整个查找第三方包的流程如下:
在当前vendor目录(如果当前目录存在vendor目录的话)查找依赖包;
如果当前目录不存在vendor目录,则到上一级目录继续查找;
重复步骤2,直到到达$GOPATH/src目录,查找vendor目录中是否存在依赖包;
如何没有查找到依赖包,则继续在$GOROOT目录查找;
如果没有查找到,则继续在$GOPATH/src目录查找。
在发布 1.6 版本时,该环境变量的值已经默认设置为 1 了,该值可以使用 go env 命令查看。在发布 1.7 版本时,已去掉该环境变量,默认开启 vendor 特性。