安装
1.brew安装的golang,需要设置GOPATH
export GOROOT=/usr/local/opt/go/libexec
2.beego api 生成命令generatedoc报错
需要设置goroot的PATH
export GOROOT=/usr/local/opt/go/libexec
export GOPATH=~/mycode/golang
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
mysql
1.连接并插入数据
package main
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
fmt.Println("Go MySQL Tutorial")
// Open up our database connection.
// I've set up a database on my local machine using phpmyadmin.
// The database is called testDb
db, err := sql.Open("mysql", "root:123456@tcp(192.168.56.112:12345)/api")
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
// defer the close till after the main function has finished
// executing
defer db.Close()
// perform a db.Query insert
insert, err := db.Query("INSERT INTO test VALUES ( 5, 'good' )")
// if there is an error inserting, handle it
if err != nil {
panic(err.Error())
}
// be careful deferring Queries if you are using transactions
defer insert.Close()
}