You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package NetUtil
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
)
/**
功能: 获取服务器的第一个网卡MAC地址
作者:黄海
时间: 2020-01-19
*/
func GetMacAddrs ( ) ( macAddrs [ ] string ) {
netInterfaces , err := net . Interfaces ( )
if err != nil {
fmt . Printf ( "fail to get net interfaces: %v" , err )
return macAddrs
}
for _ , netInterface := range netInterfaces {
macAddr := netInterface . HardwareAddr . String ( )
if len ( macAddr ) == 0 {
continue
}
macAddrs = append ( macAddrs , strings . ToUpper ( macAddr ) )
}
return macAddrs
}
// 发送GET请求
// url: 请求地址
// response: 请求返回的内容
func Get ( url string ) string {
// 超时时间: 5秒
client := & http . Client { Timeout : 5 * time . Second }
resp , err := client . Get ( url )
if err != nil {
panic ( err )
}
defer resp . Body . Close ( )
var buffer [ 512 ] byte
result := bytes . NewBuffer ( nil )
for {
n , err := resp . Body . Read ( buffer [ 0 : ] )
result . Write ( buffer [ 0 : n ] )
if err != nil && err == io . EOF {
break
} else if err != nil {
panic ( err )
}
}
return result . String ( )
}