“Go”的版本间的差异
(→Revel) |
(→Revel) |
||
第143行: | 第143行: | ||
# 如何部署生产? | # 如何部署生产? | ||
# 如何配置虚拟站点?使用nginx。 | # 如何配置虚拟站点?使用nginx。 | ||
+ | |||
+ | 启动程序 | ||
<source lang='bash'> | <source lang='bash'> | ||
D:\github_projects\revel\src>revel run github.com/tecshuttle/revel | D:\github_projects\revel\src>revel run github.com/tecshuttle/revel | ||
</source> | </source> |
2016年12月19日 (一) 08:24的版本
目录
语言相关
When is the init() function in Go (Golang) run?
var WhatIsThe = AnswerToLife() func AnswerToLife() int { return 42 } func init() { WhatIsThe = 0 } func main() { if WhatIsThe == 0 { fmt.Println("It's all a lie.") } }
AnswerToLife() is guaranteed to run before init() is called, and init() is guaranteed to run before main() is called.
Keep in mind that init() is always called, regardless if there's main or not, so if you import a package that has an init function, it will be executed.
Go examples
package main import "fmt" func main() { f() fmt.Println("Returned normally from f.") } func f() { defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }() fmt.Println("Calling g.") g(0) fmt.Println("Returned normally from g.") } func g(i int) { if i > 3 { fmt.Println("Panicking!") panic(fmt.Sprintf("%v", i)) } defer fmt.Println("Defer in g", i) fmt.Println("Printing in g", i) g(i+1) }
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:
directory for go related things: ~/programming/go directory for go compiler/tools: ~/programming/go/go-1.4 directory for go software : ~/programming/go/packages
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。
启动程序
D:\github_projects\revel\src>revel run github.com/tecshuttle/revel