GO基础环境配置详见之前写的一篇文章: https://blog.csdn.net/rclijia/article/details/94390242
本例以/opt/go为GOROOT目录,ls -l /opt/go文件列表如下:
total 216
drwxr-xr-x. 2 root root 4096 May 6 23:05 api
-rw-r--r--. 1 root root 55669 May 6 23:05 AUTHORS
drwxr-xr-x. 2 root root 29 May 6 23:07 bin
-rw-r--r--. 1 root root 1339 May 6 23:05 CONTRIBUTING.md
-rw-r--r--. 1 root root 101673 May 6 23:05 CONTRIBUTORS
drwxr-xr-x. 2 root root 80 May 6 23:05 doc
-rw-r--r--. 1 root root 5686 May 6 23:05 favicon.ico
drwxr-xr-x. 3 root root 18 May 6 23:05 lib
-rw-r--r--. 1 root root 1479 May 6 23:05 LICENSE
drwxr-xr-x. 12 root root 165 May 6 23:05 misc
-rw-r--r--. 1 root root 1303 May 6 23:05 PATENTS
drwxr-xr-x. 6 root root 76 May 6 23:08 pkg
-rw-r--r--. 1 root root 1480 May 6 23:05 README.md
-rw-r--r--. 1 root root 26 May 6 23:05 robots.txt
-rw-r--r--. 1 root root 397 May 6 23:05 SECURITY.md
drwxr-xr-x. 49 root root 4096 Jun 10 14:49 src
drwxr-xr-x. 23 root root 12288 May 6 23:05 test
drwxr-xr-x. 2 root root 6 Jun 10 14:30 vendor
-rw-r--r--. 1 root root 8 May 6 23:05 VERSION
下面简单介绍一下gopacket的入门安装配置步骤之hello world,
安装工具集: yum install binutils
然后是下载gopacket库,地址: https://github.com/google/gopacket
使用git clone也行,自行下载源码包再解压也行,
还有就是存储的目录,理论上在放哪都行,只要目录和各种PATH要匹配,
但是考虑到gopacket的基础用例的相对路径名称都是: "github.com/google/gopacket"
我把gopacket存放在: /opt/go/src/vendor/github.com/google/gopacket,
这样就不用修改import的相对路径了。
找个最简单的例子,即打印当前系统所有可用网卡的信息:
package main
import (
"fmt"
"log"
"github.com/google/gopacket/pcap"
)
func main() {
// Find all devices
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}
// Print device information
fmt.Println("Devices found:")
for _, device := range devices {
fmt.Println("\nName: ", device.Name)
fmt.Println("Description: ", device.Description)
fmt.Println("Devices addresses: ", device.Description)
for _, address := range device.Addresses {
fmt.Println("- IP address: ", address.IP)
fmt.Println("- Subnet mask: ", address.Netmask)
}
}
}
把这个文件放在任意目录,比如/tmp/test_pcap.go
在/opt/go/src目录执行: go run /tmp/test_pcap.go,
如果正确执行,说明环境配置正确:
Devices found:
Name: eth1
Description:
Devices addresses:
- IP address: 192.168.100.29
- Subnet mask: fffffc00
- IP address: 10.0.0.1
- Subnet mask: ffffff00
- IP address: fe80::44d4:7aff:feed:666e
- Subnet mask: ffffffffffffffff0000000000000000
Name: virbr0
Description:
Devices addresses:
- IP address: 192.168.122.1
- Subnet mask: ffffff00
Name: nflog
Description: Linux netfilter log (NFLOG) interface
Devices addresses: Linux netfilter log (NFLOG) interface
Name: nfqueue
Description: Linux netfilter queue (NFQUEUE) interface
Devices addresses: Linux netfilter queue (NFQUEUE) interface
Name: usbmon1
Description: USB bus number 1
Devices addresses: USB bus number 1
Name: any
Description: Pseudo-device that captures on all interfaces
Devices addresses: Pseudo-device that captures on all interfaces
Name: lo
Description:
Devices addresses:
- IP address: 127.0.0.1
- Subnet mask: ff000000
- IP address: ::1
- Subnet mask: ffffffffffffffffffffffffffffffff
有了这个例子,其他更复杂的程序请参考: https://godoc.org/github.com/google/gopacket