go-winit provides Go bindings for Libwinit, statically linking the Libwinit native library into Go applications. It enables cross-platform window creation, event handling, monitor management, native window handles, and GPU integration through Winit 0.30.
The project supports both Windows and Linux (amd64). Linux builds use the native X11/Wayland backends supplied by Winit.
In addition to exposing the libwinit C API, it provides Go callbacks, typed events, IME input, and user-event delivery from goroutines.
Winit is a cross-platform Rust library for window creation and management. It creates native windows and delivers events such as window resizing, keyboard input, mouse movement, focus changes, and window state changes.
Winit is intentionally a low-level building block. It does not provide a widget toolkit, a layout engine, or a renderer. Applications normally combine it with a graphics or UI library and use Winit's platform-specific handles when they need to draw into a window.
This repository exposes the functionality of the bundled libwinit C API to Go. The winit package keeps the native window and event-loop responsibilities while adding Go-oriented features such as typed event values, Go callbacks, IME-aware keyboard handling, and application-owned user events.
For the original Rust API and platform-specific details, see the Winit documentation and the winit::platform documentation.
The low-level Run API dispatches events using libwinit event IDs.
eventLoop.Run(func(event winit.Event) winit.ControlFlow {
switch event.Type {
case winit.WindowEventResized:
log.Printf("resized: %d x %d", event.ResizedWidth, event.ResizedHeight)
case winit.WindowEventKeyboardInput:
log.Printf("key: %s", event.LogicalKey)
case winit.WindowEventReceivedCharacter:
log.Printf("character: %q", event.ReceivedCharacterString())
case winit.WindowEventCloseRequested:
window.Close()
return winit.Exit
}
return winit.Wait
})Common event IDs include:
WindowEventResizedWindowEventMovedWindowEventCloseRequestedWindowEventKeyboardInputWindowEventCursorMovedWindowEventReceivedCharacterWindowEventScaleFactorChangedWindowEventOccludedNewEventsMainEventsClearedUserEvent
RunTyped dispatches typed event structures, so application code does not need to branch on event IDs directly.
eventLoop.RunTyped(func(event winit.EventValue) winit.ControlFlow {
switch event := event.(type) {
case winit.WindowResizedEvent:
log.Printf("resized: %d x %d", event.Width, event.Height)
case winit.WindowKeyboardInputEvent:
log.Printf("key=%q state=%v", event.LogicalKey, event.State)
case winit.WindowReceivedCharacterEvent:
log.Printf("character: %q", event.Character)
case winit.WindowOccludedEvent:
log.Printf("minimized/hidden: %t", event.Occluded)
case winit.WindowCloseRequestedEvent:
window.Close()
return winit.Exit
case winit.UserEventValue:
log.Printf("user event: %#v", event.Value)
}
return winit.Wait
})Unknown event IDs are delivered as UnknownEvent values.
WindowKeyboardInputEvent and the low-level Event contain the following keyboard information:
State:InputElementStatePressedorInputElementStateReleasedLogicalKey: a logical key name such asNamed(Enter)orCharacter("a")PhysicalKeyCode: the physical key codeHasPhysicalKey: whether a physical key code is availableIsSynthetic: whether the event was synthesized
case winit.WindowKeyboardInputEvent:
switch event.State {
case winit.InputElementStatePressed:
log.Printf("pressed: %q", event.LogicalKey)
case winit.InputElementStateReleased:
log.Printf("released: %q", event.LogicalKey)
}IME input can be enabled for each window.
window.SetIMEAllowed(true)
window.Focus()Committed multibyte text is delivered as WindowReceivedCharacterEvent values.
case winit.WindowReceivedCharacterEvent:
log.Printf("IME commit: %q", event.Character)The low-level API provides the same UTF-8 text through ReceivedCharacterString:
case winit.WindowEventReceivedCharacter:
log.Printf("IME commit: %q", event.ReceivedCharacterString())While IME is enabled, ordinary KeyboardInput events generated during IME composition are automatically suppressed by libwinit. This prevents composition keystrokes from being interpreted as regular application commands; applications receive the committed IME text instead.
The actual IME on/off mode is controlled by the Windows input method. SetIMEAllowed(true) enables IME support for the target window.
EventLoopProxy allows arbitrary Go values to be sent to the event loop from another goroutine.
proxy := eventLoop.CreateProxy()
defer proxy.Close()
go func() {
proxy.Send(struct {
Message string
}{Message: "event from goroutine"})
}()
eventLoop.RunTyped(func(event winit.EventValue) winit.ControlFlow {
switch event := event.(type) {
case winit.UserEventValue:
log.Printf("user event: %#v", event.Value)
}
return winit.Wait
})Send queues the value on the Go side and wakes the libwinit event loop. The user-event callback runs on the event-loop thread.
Minimize and restore transitions are delivered as WindowOccludedEvent values. Maximization and restoration are delivered as resize events, so use Window.IsMaximized() to determine the current state.
case winit.WindowResizedEvent:
if window.IsMaximized() {
log.Println("window maximized")
}
case winit.WindowOccludedEvent:
log.Printf("window occluded: %t", event.Occluded)examples/simple: event-ID dispatch, IME, and keyboard inputexamples/event_types: typed event switches, IME, keyboard input, and user events
go run ./examples/simple
go run ./examples/event_typesgo test ./...When verifying the Linux example under WSLg, do not force X11 with
WINIT_UNIX_BACKEND=x11; let the example use its default backend selection.
On systems where the WSLg display bridge is unavailable, the process may keep
running while no window is displayed. Check the WSLg/GUI integration before
debugging the Go or Libwinit code.
The bundled Rust crate produces a Windows GNU static library in both profiles. Install the Rust target first if it is not already available:
rustup target add x86_64-pc-windows-gnuBuild both profiles with:
make libwinit-staticThe libraries are written to build/libwinit/x86_64-pc-windows-gnu/debug/libwinit.a
and build/libwinit/x86_64-pc-windows-gnu/release/libwinit.a. Individual profiles
can be built with make libwinit-debug or make libwinit-release.