Everything is Gray

A place to put stuff.

Get Mac Serial Number Programmatically with Cgo

| Comments

I recently had the occassion to obtain the serial number from a Mac within a Go program. Rather than shelling out to system_profiler or ioreg, I opted to use Cgo and retrieve the serial number via the IOKit framework. Here’s how that looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

// #cgo LDFLAGS: -framework CoreFoundation -framework IOKit
// #include <CoreFoundation/CoreFoundation.h>
// #include <IOKit/IOKitLib.h>
//
// const char *
// getSerialNumber()
// {
//     CFMutableDictionaryRef matching = IOServiceMatching("IOPlatformExpertDevice");
//     io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
//     CFStringRef serialNumber = IORegistryEntryCreateCFProperty(service,
//         CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0);
//     const char *str = CFStringGetCStringPtr(serialNumber, kCFStringEncodingUTF8);
//     IOObjectRelease(service);
//
//     return str;
// }
import "C"

import (
  "fmt"
)

func main() {
  serialNumber := C.GoString(C.getSerialNumber())
  fmt.Println(serialNumber)
}

Make sure that the import C line goes directly below the C code, otherwise it will fail with something like the following:

1
./get_mac_serial_number.go:26:29: could not determine kind of name for C.getSerialNumber

Hopefully this helps the next time you need to get the serial number of a Mac in Golang!

Comments