Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
macOS/iOS: Writing to NSLog using Golang (using Cgo)
package myapp
import "log"
mobilelog := MobileLogger{}
logger := log.New(mobilelog, "", 0)
package myapp
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#import <Foundation/Foundation.h>
void Log(const char *text) {
NSString *nss = [NSString stringWithUTF8String:text];
NSLog(@"%@", nss);
import "C"
import "unsafe"
type MobileLogger struct {
func (nsl MobileLogger) Write(p []byte) (n int, err error) {
p = append(p, 0)
cstr := (*C.char)(unsafe.Pointer(&p[0]))
return len(p), nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment