Help us shape the future of Go! Take the 2026 Go Developer Survey

cocoa

package
v0.0.0-...-48061bc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Rendered for darwin/amd64

Overview

Package cocoa is the small part of the Objective-C runtime that package keychain needs: building NSString, NSData and NSDictionary values, reading bytes back out, and running work inside an autorelease pool.

Nothing here uses cgo. purego resolves symbols with dlopen and dlsym and implements the C calling convention in assembly, so a program importing package keychain still builds with CGO_ENABLED=0 and still cross-compiles.

Together with package keychain this is the only place in the module that imports purego.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Array

func Array(items []objc.ID) objc.ID

Array builds an autoreleased NSArray from already-bridged objects.

func Autorelease

func Autorelease(id objc.ID) objc.ID

Autorelease adds an object to the current autorelease pool.

func Bool

func Bool(v bool) objc.ID

Bool boxes a bool as an autoreleased NSNumber.

func Count

func Count(array objc.ID) uint64

Count returns an NSArray's length.

func Data

func Data(b []byte) objc.ID

Data wraps a Go byte slice in an autoreleased NSData. The bytes are copied, so the caller's slice may be reused immediately.

func Dict

func Dict(pairs ...objc.ID) objc.ID

Dict builds an autoreleased NSMutableDictionary from alternating keys and values. It is toll-free bridged to the CFDictionaryRef the Security functions expect, so it is passed to them as is.

func GoBytes

func GoBytes(id objc.ID) []byte

GoBytes copies an NSData into a Go slice.

func Load

func Load() error

Load maps the Foundation and Security frameworks. It is safe to call from many goroutines and does its work once.

func ObjectAt

func ObjectAt(array objc.ID, i uint64) objc.ID

ObjectAt returns an NSArray's element at index i.

func Release

func Release(id objc.ID)

Release decrements an object's reference count.

func String

func String(s string) objc.ID

String converts a Go string to an autoreleased NSString, or 0 if it cannot be carried: invalid UTF-8, which +stringWithUTF8String: rejects by returning nil, or an interior NUL, which the C string API would silently treat as the end.

func WithPool

func WithPool(fn func())

WithPool runs fn inside an autorelease pool. Every goroutine that touches the Objective-C runtime must go through this, or the objects it creates leak for the life of the process.

The goroutine is locked to its OS thread for the duration, and that is not a precaution — it is required for correctness. Autorelease pools are thread-local: pushing returns a token that belongs to the calling thread's pool stack, and popping must hand it back to that same thread. A goroutine has no such affinity; the scheduler may move it to another thread at any preemption point, a function call being enough. Without the lock a pool can be pushed on one thread and popped on another, which corrupts the other thread's pool stack and surfaces later, somewhere unrelated, as an over-released object or an "Attempt to use unknown class" from the runtime.

LockOSThread nests, so nested calls behave correctly.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL