go 环境安装

ubuntu 上安装go

sudo apt-get install golang-go

但可能不是最新版。

可到 https://golang.org/dl/ 7 下载最新版,参考官方安装文档 https://golang.org/doc/install 1

ok

#vi ~/.bashrc

# for go lang
export GOROOT=/usr/local/go
export GOPATH=/mnt/goprojects
export GOOS=linux
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

export GOROOT=xxxx/go
export GOOS=linux
export PATH=$PATH:$GOROOT/bin


$ cd go/src
$ ./all.bash
$ export PATH=$PATH:/home/yourname/download/go/bin
$ export GOROOT=/home/yourname/download/go
$ go version
go version go1.4.2 linux/386

你如果安装Go在你的Home目录下,你应该$HOME/.profile 或 .bashrc 文件增加下面设置。

export GOROOT=$HOME/download/go  --go 的安装路径
export GOPATH=/home/yourname/gocode  -- 自己准备存放go 的项目代码
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

测试是否安装成功

建立 文件hello.go

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

运行

$ go run hello.go
hello, world

安装go web 框架 martini https://github.com/go-martini/martini 2

cd goprojects/
go get github.com/go-martini/martini

测试

新建 server.go

package main

import "github.com/go-martini/martini"

func main() {
  m := martini.Classic()
  m.Get("/", func() string {
    return "Hello world! go martini"
  })
  m.Run()
}

go run server.go

$ go run server.go
[martini] listening on :3000 (development)

这时你将会有一个Martini的服务监听了, 地址是: localhost:3000

ssdb go client

go get github.com/eryx/lessgo/data/hissdb

https://github.com/lessos/lessgo/tree/master/data/hissdb 1

sample

package main

import (
    "fmt"
    "github.com/eryx/lessgo/data/hissdb"
)

func main() {

    conn, err := hissdb.NewConnector(hissdb.Config{
        Host:    "127.0.0.1",
        Port:    6380,
        Timeout: 3,  // timeout in second, default to 10
        MaxConn: 10, // max connection number, default to 1
    })
    if err != nil {
        fmt.Println("Connect Error:", err)
        return
    }
    defer conn.Close()

    // API::Bool() bool
    conn.Cmd("set", "true", "True")
    if conn.Cmd("get", "true").Bool() {
        fmt.Println("set bool OK")
    }

    conn.Cmd("set", "aa", "val-aaaaaaaaaaaaaaaaaa")
    conn.Cmd("set", "bb", "val-bbbbbbbbbbbbbbbbbb")
    conn.Cmd("set", "cc", "val-cccccccccccccccccc")
    // API::String() string
    if rs := conn.Cmd("get", "aa"); rs.State == "ok" {
        fmt.Println("get OK\n\t", rs.String())
    }
    // API::Hash() []Entry
    if rs := conn.Cmd("multi_get", "aa", "bb"); rs.State == "ok" {
        fmt.Println("multi_get OK")
        for _, v := range rs.Hash() {
            fmt.Println("\t", v.Key, v.Value)
        }
    }
    if rs := conn.Cmd("scan", "aa", "cc", 10); rs.State == "ok" {
        fmt.Println("scan OK")
        for _, v := range rs.Hash() {
            fmt.Println("\t", v.Key, v.Value)
        }
    }

    conn.Cmd("zset", "z", "a", 3)
    conn.Cmd("multi_zset", "z", "b", -2, "c", 5, "d", 3)
    if rs := conn.Cmd("zrscan", "z", "", "", "", 10); rs.State == "ok" {
        fmt.Println("zrscan OK")
        for _, v := range rs.Hash() {
            fmt.Println("\t", v.Key, v.Value)
        }
    }

    conn.Cmd("set", "key", 10)
    if rs := conn.Cmd("incr", "key", 1).Int(); rs > 0 {
        fmt.Println("incr OK\n\t", rs)
    }

    // API::Int() int
    // API::Int64() int64
    conn.Cmd("setx", "key", 123456, 300)
    if rs := conn.Cmd("ttl", "key").Int(); rs > 0 {
        fmt.Println("ttl OK\n\t", rs)
    }

    if rs := conn.Cmd("multi_hset", "zone", "c1", "v-01", "c2", "v-02"); rs.State == "ok" {
        fmt.Println("multi_hset OK")
    }
    if rs := conn.Cmd("multi_hget", "zone", "c1", "c2"); rs.State == "ok" {
        fmt.Println("multi_hget OK")
        for _, v := range rs.Hash() {
            fmt.Println("\t", v.Key, v.Value)
        }
    }

    // API::Float64() float64
    conn.Cmd("set", "float", 123.456)
    if rs := conn.Cmd("get", "float").Float64(); rs > 0 {
        fmt.Println("float OK\n\t", rs)
    }

    // API::List() []string
    conn.Cmd("qpush", "queue", "q-1111111111111")
    conn.Cmd("qpush", "queue", "q-2222222222222")
    if rs := conn.Cmd("qpop", "queue", 10); rs.State == "ok" {
        fmt.Println("qpop OK")
        for k, v := range rs.List() {
            fmt.Println("\t", k, v)
        }
    }
}

ubuntu 安装方法

下载 https://golang.org/dl/ 7

wget https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz

sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

vi ~/.profile

export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/goprojects
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Relative Articles