Cocoa by cgo

1,382 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,382
On SlideShare
0
From Embeds
0
Number of Embeds
158
Actions
Shares
0
Downloads
8
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Cocoa by cgo

  1. 1. Cocoa by cgo Hajime Hoshi (星一)
  2. 2. About Myself • Hajime Hoshi (星一) • Software Engineer
  3. 3. What’s cgo? • The special library/tool to use C programs • // void HelloWorld() { // printf("Hello, World!n"); // } import "C" func main() { C.HelloWorld() }
  4. 4. Theme • I wanna develop Mac OS X applications in Go! • The more Go, the more merrier • So, can cgo treat Objective-C? • YES!!!!
  5. 5. package main   // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa // #import <Cocoa/Cocoa.h> // // void run() { // @autoreleasepool { // NSApplication* app = [NSApplication sharedApplication]; // NSWindow* window = [[NSWindow alloc] // initWithContentRect:NSMakeRect(0,0,200,200) // styleMask:NSTitledWindowMask // backing:NSBackingStoreBuffered // defer:NO]; // [window orderFrontRegardless]; // [app run]; // } // } // import "C"   func main() { C.run() }
  6. 6. package main   // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa // #import <Cocoa/Cocoa.h> // // void run() { // @autoreleasepool { // NSApplication* app = [NSApplication sharedApplication]; // NSWindow* window = [[NSWindow alloc] // initWithContentRect:NSMakeRect(0,0,200,200) // styleMask:NSTitledWindowMask // backing:NSBackingStoreBuffered // defer:NO]; // [window orderFrontRegardless]; // [app run]; // } // } // import "C"   func main() { C.run() } // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa
  7. 7. package main   // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa // #import <Cocoa/Cocoa.h> // // void run() { // @autoreleasepool { // NSApplication* app = [NSApplication sharedApplication]; // NSWindow* window = [[NSWindow alloc] // initWithContentRect:NSMakeRect(0,0,200,200) // styleMask:NSTitledWindowMask // backing:NSBackingStoreBuffered // defer:NO]; // [window orderFrontRegardless]; // [app run]; // } // } // import "C"   func main() { C.run() } // #import <Cocoa/Cocoa.h>
  8. 8. package main   // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa // #import <Cocoa/Cocoa.h> // // void run() { // @autoreleasepool { // NSApplication* app = [NSApplication sharedApplication]; // NSWindow* window = [[NSWindow alloc] // initWithContentRect:NSMakeRect(0,0,200,200) // styleMask:NSTitledWindowMask // backing:NSBackingStoreBuffered // defer:NO]; // [window orderFrontRegardless]; // [app run]; // } // } // import "C"   func main() { C.run() }
  9. 9. package main   // #cgo CFLAGS: -x objective-c // #cgo LDFLAGS: -framework Cocoa // #import <Cocoa/Cocoa.h> // // void run() { // @autoreleasepool { // NSApplication* app = [NSApplication sharedApplication]; // NSWindow* window = [[NSWindow alloc] // initWithContentRect:NSMakeRect(0,0,200,200) // styleMask:NSTitledWindowMask // backing:NSBackingStoreBuffered // defer:NO]; // [window orderFrontRegardless]; // [app run]; // } // } // import "C"   func main() { C.run() } import "C"   func main() { C.run() }
  10. 10. Demo
  11. 11. Appendix • As of Go 1.2? • Objective-C couldn’t be compiled in Go 1.1. • I’m not sure this is an official feature.
  12. 12. Appendix • Be careful for threads! • Calling Cocoa functions from not- appropriate goroutines might crash the application.
  13. 13. Appendix • Long compiling time! (because of Objective-C)
  14. 14. Appendix • Go command recognizes *.c files, but not *.m (or *.mm) files. • You need to write Objective-C program in C files (Issue 6536). Sigh...
  15. 15. Oshimai

×