這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
之前都是簡單測試,沒有按照go建議的開發規範來,以後要正規化。
go的開發環境規範,參見doc/code
中文連結 http://docscn.studygolang.com/doc/code.html
1.下載並安裝go
http://www.golangtc.com/download
最新的1.6.2 已經80M了。 1.1才 40M.可見go的成長還是很迅速的。
設定環境變數%GOPATH%
2。下載並安裝git用戶端
得到一個Git bash.可以在windows下用linux命令。就是很慢。
3.安裝godep
在git bash中,執行go get github.com/tools/godep 產生godep.exe在$GOPATH/bin下,複製到go的安裝運行目錄. 或者把$GOPATH/bin加到$PATH4.建立go項目(pakage)以snmp為例在$GOPATH下建立目錄/src ,/bin, /pkg在src目錄下建立psnmp, gosnmp從github下載https://github.com/soniah/gosnmp,並展開到gosnmp目錄在psnmp目錄下建立tsnmp.go
// Copyright 2012-2014 The GoSNMP Authors. All rights reserved. Use of this// source code is governed by a BSD-style license that can be found in the// LICENSE file.package mainimport ("fmt""log"//g "github.com/soniah/gosnmp"g "gosnmp")func main() {// Default is a pointer to a GoSNMP struct that contains sensible defaults// eg port 161, community public, etcg.Default.Target = "192.168.6.87"err := g.Default.Connect()if err != nil {log.Fatalf("Connect() err: %v", err)}defer g.Default.Conn.Close()//oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}//test asciioids := []string{"1.3.6.1.2.1.2.2.1.6.0"};result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDSif err2 != nil {log.Fatalf("Get() err: %v", err2)}for i, variable := range result.Variables {fmt.Printf("%d: oid: %s ", i, variable.Name)// the Value of each variable returned by Get() implements// interface{}. You could do a type switch...switch variable.Type {case g.OctetString:fmt.Printf("string: %s\n", string(variable.Value.([]byte)))default:// ... or often you're just interested in numeric values.// ToBigInt() will return the Value as a BigInt, for plugging// into your calculations.fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))}}}
5.編譯執行
可以直接運行go run src/psnmp/tsnmp.go可以直接編譯(直接在$GOPATH下產生psnmp.exe)go build psnmp可以安裝(在$GOPATH/bin下產生psnmp.exe)go install psnmp 6.使用godep儲存原始碼這個需要提交到配置庫才行,待續。