dongyaobo9081 2014-01-09 11:19
浏览 372
已采纳

在Go中生成MAC地址

I'm looking for an example on how to generate a MAC Address in Go. I have found many examples on creating UUIDs but nothing on MAC addresses.

Can someone help?

Thanks, Ben

  • 写回答

1条回答 默认 最新

  • dthyxna3894 2014-01-09 17:44
    关注

    Here is how I would do it (playground)

    import (
        "crypto/rand"
        "fmt"
    )
    
    func main() {
        buf := make([]byte, 6)
        _, err := rand.Read(buf)
        if err != nil {
            fmt.Println("error:", err)
            return
        }
        // Set the local bit
        buf[0] |= 2
        fmt.Printf("Random MAC address: %02x:%02x:%02x:%02x:%02x:%02x
    ", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
    }
    

    Note the setting of the local bit which means it won't clash with any globally administered addresses (see wikipedia for more info)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题