windows环境go开环境实例

之前都是简单测试,没有按照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保存源代码
这个需要提交到配置库才行,待续。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值