cilium 1.15.1
生成随机mac
package main
import (
"crypto/rand"
"fmt"
"net"
)
// MAC address is an net.HardwareAddr encapsulation to force cilium to only use MAC-48.
type MAC net.HardwareAddr
// String returns the string representation of m.
func (m MAC) String() string {
return net.HardwareAddr(m).String()
}
// GenerateRandMAC generates a random unicast and locally administered MAC address.
func GenerateRandMAC() (MAC, error) {
buf := make([]byte, 6)
if _, err := rand.Read(buf); err != nil {
return nil, fmt.Errorf("Unable to retrieve 6 rnd bytes: %s", err)
}
// Set locally administered addresses bit and reset multicast bit
buf[0] = (buf[0] | 0x02) & 0xfe
return buf, nil
}
func main() {
if mac, err := GenerateRandMAC(); err == nil {
fmt.Println(mac)
}
if mac, err := GenerateRandMAC(); err == nil {
fmt.Println(mac)
}
}
标签:err,nil,fmt,MAC,mac,go,操作,buf
From: https://www.cnblogs.com/WJQ2017/p/18238437