“Go”的版本间的差异
(→Revel) |
(→周边使用) |
||
第31行: | 第31行: | ||
=周边使用= | =周边使用= | ||
+ | |||
+ | ==what should be the values of GOPATH and GOROOT?== | ||
+ | |||
+ | Here is a my simple setup: | ||
+ | |||
+ | <soruce lang='text'> | ||
+ | directory for go related things: ~/programming/go | ||
+ | directory for go compiler/tools: ~/programming/go/go-1.4 | ||
+ | directory for go software : ~/programming/go/packages | ||
+ | </soruce> | ||
+ | |||
+ | GOROOT, GOPATH, PATH are set as following: | ||
+ | |||
+ | <source lang='bash'> | ||
+ | export GOROOT=/home/user/programming/go/go-1.4 | ||
+ | export GOPATH=/home/user/programming/go/packages | ||
+ | export PATH=$PATH:$GOROOT/bin:$GOPATH/bin | ||
+ | </source> | ||
+ | |||
+ | So, in short: | ||
+ | |||
+ | GOROOT is for compiler/tools that comes from go installation. | ||
+ | |||
+ | GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get"). | ||
==What's the recommended way to connect to MySQL from Go?== | ==What's the recommended way to connect to MySQL from Go?== |
2016年9月28日 (三) 01:57的版本
目录
语言相关
Array、Slice、Map和Set使用详解
http://www.jb51.net/article/56828.htm
- 数组是 slice 和 map 的底层结构。
- slice 是 Go 里面惯用的集合数据的方法,map 则是用来存储键值对。
- 内建函数 make 用来创建 slice 和 map,并且为它们指定长度和容量等等。slice 和 map 字面值也可以做同样的事。
- slice 有容量的约束,不过可以通过内建函数 append 来增加元素。
- map 没有容量一说,所以也没有任何增长限制。
- 内建函数 len 可以用来获得 slice 和 map 的长度。
- 内建函数 cap 只能作用在 slice 上。
- 可以通过组合方式来创建多维数组和 slice。map 的值可以是 slice 或者另一个 map。slice 不能作为 map 的键。
- 在函数之间传递 slice 和 map 是相当廉价的,因为他们不会传递底层数组的拷贝。
How to print struct variables in console?
fmt.Printf("%+v\n", users)
From the fmt package:
when printing structs, the plus flag (%+v) adds field names.
IDE
- idea安装go插件(推荐)
- sublime + gosublime
周边使用
what should be the values of GOPATH and GOROOT?
Here is a my simple setup:
<soruce lang='text'> directory for go related things: ~/programming/go directory for go compiler/tools: ~/programming/go/go-1.4 directory for go software : ~/programming/go/packages </soruce>
GOROOT, GOPATH, PATH are set as following:
export GOROOT=/home/user/programming/go/go-1.4 export GOPATH=/home/user/programming/go/packages export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
So, in short:
GOROOT is for compiler/tools that comes from go installation.
GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").
What's the recommended way to connect to MySQL from Go?
A few drivers are available but you should only consider those that implement the database/sql API as
- it provides a clean and efficient syntax,
- it ensures you can later change the driver without changing your code, apart the import and connection.
Two fast and reliable drivers are available for MySQL :
- MyMySQL
- Go-MySQL-Driver
I've used both of them in production, programs are running for months with connection numbers in the millions without failure.
Revel
- 路由、控制器、模板概览
- 控制器里的变量传给模板
- 以输出json:array、struct、slice操作
- 连接数据库、把数据库数据整理为struct
- 在控制台显示带键值的struct,fmt.Printf()
- 输出json,键值首字母要大写,否则内容为空。
- 在模板显示数据库记录。
- 用生产模式运行项目
其它疑问
- 如何编译linux版本?
- 如何部署生产?
- 如何配置虚拟站点?使用nginx。