之前都是简单测试,没有按照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加到$PATH
4.创建go项目(pakage)
以snmp为例
在$GOPATH下创建目录/src ,/bin, /pkg
在src目录下创建psnmp, 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 main
import (
"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, etc
g.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 ascii
oids := []string{"1.3.6.1.2.1.2.2.1.6.0"};
result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
if 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保存源代码
这个需要提交到配置库才行,待续。