学习GO第一天,自我感觉可麻利的开干了-GO语言配置、开发、服务器部署
第一步下载 go sdk
https://golang.org/dl/
https://storage.googleapis.com/golang/go1.7.4.linux-amd64.tar.gz
解压go到/usr/local 目录下
tar -C /usr/local -xzf go1.7.4.linux-amd64.tar.gz
第二步安装IntelliJ IDEA 2016.2.4(64)
linux环境设置
1.设置环境变量在/etc/profile 中
#go set
export GOROOT=/usr/local/go
export GOOS=linux
export GOARCH=386
export GOBIN=$GOROOT/bin
PATH=$PATH:$GOBIN
export PATH
#alias cdg="cd /opt/go"
2、http服务范例
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi, This is an example of http service in golang!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
或:
func hello(c web.C, w http.ResponseWriter, r *http.Request){
name := r.PostFormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
Should be this:
func hello(c web.C, w http.ResponseWriter, r *http.Request){
//Call to ParseForm makes form fields available.
err := r.ParseForm()
if err != nil {
// Handle error here via logging and then return
}
name := r.PostFormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
go开源项目 http://www.open-open.com/lib/view/open1396063913278.html#Caching
安装mysql驱动:
1、cd d:\go\src
2、git clone https://github.com/go-sql-driver/mysql.git
go mysql编程范例
package main
import (
"fmt"
"database/sql"
//"database/sql/driver"
_"mysql"
//_ "MYSQL"
)
type TestMysql struct {
db *sql.DB
}
/* 初始化数据库引擎 */
func Init() (*TestMysql,error){
test := new(TestMysql);
//mysql.MySQLDriver{}
db,err := sql.Open("mysql","root:wendy197626@/test");
//第一个参数 : 数据库引擎
//第二个参数 : 数据库DSN配置。Go中没有统一DSN,都是数据库引擎自己定义的,因此不同引擎可能配置不同
//本次演示采用http://code.google.com/p/go-mysql-driver
if err!=nil {
fmt.Println("database initialize error : ",err.Error());
return nil,err;
}
test.db = db;
return test,nil;
}
func (test *TestMysql)CreateDB(dbname string){
if test.db==nil {
return;
}
stmt,err := test.db.Prepare("create table test(id bigint NOT NULL AUTO_INCREMENT , name varchar(20), age int, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8; ");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer stmt.Close();
if result,err := stmt.Exec();err==nil {
if id,err := result.LastInsertId();err==nil {
fmt.Println("insert id : ",id);
}
}
}
/* 测试数据库数据添加 */
func (test *TestMysql)Create(){
if test.db==nil {
return;
}
stmt,err := test.db.Prepare("insert into test(name,age)values(?,?)");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer stmt.Close();
if result,err := stmt.Exec("张三",20);err==nil {
if id,err := result.LastInsertId();err==nil {
fmt.Println("insert id : ",id);
}
}
if result,err := stmt.Exec("李四",30);err==nil {
if id,err := result.LastInsertId();err==nil {
fmt.Println("insert id : ",id);
}
}
if result,err := stmt.Exec("王五",25);err==nil {
if id,err := result.LastInsertId();err==nil {
fmt.Println("insert id : ",id);
}
}
}
/* 测试数据库数据更新 */
func (test *TestMysql)Update(){
if test.db==nil {
return;
}
stmt,err := test.db.Prepare("update test set name=?,age=? where age=?");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer stmt.Close();
if result,err := stmt.Exec("周七",40,25);err==nil {
if c,err := result.RowsAffected();err==nil {
fmt.Println("update count : ",c);
}
}
}
/* 测试数据库数据读取 */
func (test *TestMysql)Read(){
if test.db==nil {
return;
}
rows,err := test.db.Query("select id,name,age from test limit 0,5");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer rows.Close();
fmt.Println("");
cols,_ := rows.Columns();
for i := range cols {
fmt.Print(cols[i]);
fmt.Print("\t");
}
fmt.Println("");
var id int;
var name string;
var age int;
for rows.Next(){
if err := rows.Scan(&id,&name,&age);err==nil {
fmt.Print(id);
fmt.Print("\t");
fmt.Print(name);
fmt.Print("\t");
fmt.Print(age);
fmt.Print("\t\r\n");
}
}
}
/* 测试数据库删除 */
func (test *TestMysql)Delete(){
if test.db==nil {
return;
}
stmt,err := test.db.Prepare("delete from test where age=?");
if err!=nil {
fmt.Println(err.Error());
return;
}
defer stmt.Close();
if result,err := stmt.Exec(20);err==nil {
if c,err := result.RowsAffected();err==nil{
fmt.Println("remove count : ",c);
}
}
}
func (test *TestMysql)Close(){
if test.db!=nil {
test.db.Close();
}
}
func main(){
test,err := Init();
println(test,err)
if err==nil {
println("asds")
test.CreateDB("test") //建表
test.Create();
test.Update();
test.Read();
test.Delete();
test.Read();
test.Close();
}
}