“Go”的版本间的差异

来自tomtalk
跳转至: 导航搜索
What's the recommended way to connect to MySQL from Go?
Tom讨论 | 贡献
IDE
第82行: 第82行:
  
 
when printing structs, the plus flag (%+v) adds field names.
 
when printing structs, the plus flag (%+v) adds field names.
 +
 +
=使用=
 +
 +
==无法import github的包==
 +
 +
<source lang='bash'>
 +
$ go get github.com/go-sql-driver/mysql
 +
</source>
  
 
=IDE=
 
=IDE=

2016年12月20日 (二) 08:21的版本

语言相关

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

  1. 数组是 slice 和 map 的底层结构。
  2. slice 是 Go 里面惯用的集合数据的方法,map 则是用来存储键值对。
  3. 内建函数 make 用来创建 slice 和 map,并且为它们指定长度和容量等等。slice 和 map 字面值也可以做同样的事。
  4. slice 有容量的约束,不过可以通过内建函数 append 来增加元素。
  5. map 没有容量一说,所以也没有任何增长限制。
  6. 内建函数 len 可以用来获得 slice 和 map 的长度。
  7. 内建函数 cap 只能作用在 slice 上。
  8. 可以通过组合方式来创建多维数组和 slice。map 的值可以是 slice 或者另一个 map。slice 不能作为 map 的键。
  9. 在函数之间传递 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.

使用

无法import github的包

$ go get github.com/go-sql-driver/mysql

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 :

I've used both of them in production, programs are running for months with connection numbers in the millions without failure.

Revel

  1. 路由、控制器、模板概览
  2. 控制器里的变量传给模板
  3. 以输出json:array、struct、slice操作
  4. 连接数据库、把数据库数据整理为struct
  5. 在控制台显示带键值的struct,fmt.Printf()
  6. 输出json,键值首字母要大写,否则内容为空。
  7. 在模板显示数据库记录。
  8. 用生产模式运行项目

其它疑问

  1. 如何编译linux版本?
  2. 如何部署生产?
  3. 如何配置虚拟站点?使用nginx。

启动程序

D:\github_projects\revel\src>revel run github.com/tecshuttle/revel