Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Go言語からCocoaを使ってみた
Search
matuyuji
August 09, 2014
Programming
130
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Go言語からCocoaを使ってみた
matuyuji
August 09, 2014
More Decks by matuyuji
See All by matuyuji
Emacs × Touch Bar
matuyuji
2
1.9k
ARKit + SceneKitでMinesweeperを作ってみた
matuyuji
1
830
Go + QtでiOS アプリ開発
matuyuji
0
430
@_specialized なお話し
matuyuji
0
520
Xcode Souce Code Extensionを使ってみた
matuyuji
0
460
Codebeatを 試してみた
matuyuji
0
830
React Nativeで UIコンポーネントをつくる
matuyuji
0
1.1k
React Nativeを使ってみた
matuyuji
0
1.4k
SwiftでLens
matuyuji
1
1.1k
Other Decks in Programming
See All in Programming
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
530
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
300
Go 1.27 における memory allocation の高速化
andpad
0
200
今さら聞けない .NET CLI
htkym
0
200
AIが無かった頃の素敵な出会いの話
codmoninc
1
450
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.6k
yield再入門 #phpcon
o0h
PRO
0
1.1k
全PRの83%がAIレビューだけでマージできるようになった開発組織はその後どうなったか
athug
1
1.7k
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
React本体のコードリーディング
high_g_engineer
1
150
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
1
140
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
520
Featured
See All Featured
My Coaching Mixtape
mlcsv
0
240
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
New Earth Scene 8
popppiees
3
2.5k
Odyssey Design
rkendrick25
PRO
2
760
Ruling the World: When Life Gets Gamed
codingconduct
0
300
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
420
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
670
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Writing Fast Ruby
sferik
630
63k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Transcript
(Pݴޠ͔Β$PDPB ΛͬͯΈͨ @matuyuji ! Cocoaษڧձؔ 2014.8.9
!NBUVZVKJ w TBGYEFWCMPHTQPUKQ w &NBDT͍ w 3FBDUJWF$PDPBؔ
(P͔Β$PDPBΛ͍͍ͨ
! func main() { app := cocoa.NSApplication_sharedApplication() win := cocoa.NSWindow_initWithContentRectStyleMaskBackingDefer(
cocoa.NSRect{0,0, 200,200}, NSTitledWindowMask, NSBackingStoreBuffered, false) win.CascadeTopLeftFromPoint(cocoa.NSPoint{20,20}) win.SetTitle(cocoa.NSString_initWithUTF8String("SomeApp")) win.MakeKeyAndOrderFront(nil) app.ActivateIgnoringOtherApps(true) ! fmt.Println(app, win) fmt.Println(app.GetClassName(), win.GetClassName()) ! app.Run(); } ͜ͷίʔυ͕ಈ͘Α͏ʹ͍ͨ͠ʂ
(P͔Β0CKFDUJWF$ ΛݺͿํ๏
$HP
$HP w (P͔Β$ݴޠίʔυΛݺͼग़͢Έ w ΘΓͱ͍ͮΒ͍
1BDLBHFz$z ! package main ! // #include <stdio.h> import "C"
! func main() { C.puts(C.CString("hello, world!")) } લͷίϝϯτ͕ ύοέʔδCͷͨΊͷ ίʔυʹͳΔ (Pจࣈྻˠ$จࣈྻ $ݴޠͷTUEJPIͷؔ
1BDLBHF$ )FMQFSGVODUJPO • func C.CString(string) *C.char (Pจࣈྻˠ$จࣈྻ • func C.GoString(*C.char)
string $จࣈྻˠ(Pจࣈྻ • func C.GoBytes(unsafe.Pointer, C.int) []byte $όΠτྻˠ(PόΠτྻ
! package main ! /* #include <stdio.h> ! char* hello()
{ return "hello, world!"; } */ import "C" ! func main() { C.puts(C.hello()) }
! package main ! /* int add(int x, int y)
{ return x + y; } */ import "C" import "fmt" ! func main() { fmt.Println(C.add(2, 3)) }
HPCVJMEYGPPHP foo.go foo.cgo1.go foo.cgo2.c Object Object Object cgo Binary 6l
6g clang pack _cgo_defun.c Object 6c ࣮ࡍͬͱෳࡶ‥
(PDPNNBOET 6a 8a 5a 6c 8c 5c 6g 8g 5g
6l 8l 5l assembler c compiler go compiler linker 6 (x86-64) 8 (x86-32) 5 (ARM) Plan 9 ༝དྷ ΞʔΩςΫνϟ πʔϧ
HPCVJMEYGPPHP foo.go foo.cgo1.go foo.cgo2.c Object Object Object cgo Binary 6l
6g clang pack _cgo_defun.c Object 6c
! void _cgo_2434c0b5a67e_Cfunc_add(void *v) { struct { int p0; int
p1; int r; char __pad12[4]; } __attribute__((__packed__)) *a = v; a->r = add(a->p0, a->p1); } ! int add(int x, int y) { return x + y; } ! void ɾ_Cfunc_add(struct{void *y[2];}p) { runtimeɾcgocall(_cgo_2434c0b5a67e_Cfunc_add, &p); } foo.cgo2.c _cgo_defun.c
HPCVJMEYGPPHP foo.go foo.cgo1.go foo.cgo2.c Object Object Object cgo Binary 6l
6g clang pack _cgo_defun.c Object 6c
! package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:
-framework Foundation #import <Cocoa/Cocoa.h> ! NSString* NSString_initWithUTF8String(char* text) { return [[NSString alloc] initWithUTF8String:text]; } ! const char* NSString_descripton(NSString* obj) { return [[obj description] UTF8String]; } */ import "C" import "fmt" ! func main() { str := C.NSString_initWithUTF8String(C.CString("foobar")) fmt.Println(C.GoString(C.NSString_descripton(str))) }
! package main /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS:
-framework Cocoa #import <Cocoa/Cocoa.h> int StartApp(void) { [NSAutoreleasePool new]; [NSApplication sharedApplication]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; id menubar = [[NSMenu new] autorelease]; id appMenuItem = [[NSMenuItem new] autorelease]; [menubar addItem:appMenuItem]; [NSApp setMainMenu:menubar]; id appMenu = [[NSMenu new] autorelease]; id appName = [[NSProcessInfo processInfo] processName]; id quitTitle = [@"Quit " stringByAppendingString:appName]; id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@“q"] autorelease]; [appMenu addItem:quitMenuItem]; [appMenuItem setSubmenu:appMenu]; id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO] autorelease]; [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; [window setTitle:appName]; [window makeKeyAndOrderFront:nil]; [NSApp activateIgnoringOtherApps:YES]; [NSApp run]; return 0; } */ import "C" ! func main() { C.StartApp() } LBWVFYBNQMFHP ˒
! func main() { app := cocoa.NSApplication_sharedApplication() win := cocoa.NSWindow_initWithContentRectStyleMaskBackingDefer(
cocoa.NSRect{0,0, 200,200}, NSTitledWindowMask, NSBackingStoreBuffered, false) win.CascadeTopLeftFromPoint(cocoa.NSPoint{20,20}) win.SetTitle(cocoa.NSString_initWithUTF8String("SomeApp")) win.MakeKeyAndOrderFront(nil) app.ActivateIgnoringOtherApps(true) ! fmt.Println(app, win) fmt.Println(app.GetClassName(), win.GetClassName()) ! app.Run(); }
TBGYDMBOHPCKDHP w $MBOH1ZUIPO#JOEJOHTΛར༻ w 0CKFDUJWF$ͷϔομ͔Β(PίʔυΛੜ python scripts/clang-objcgo.py import.h \ >
src/cocoa/import.go go build main ./main
$MBOH1ZUIPO#JOEJOHT w ιʔείʔυ͔Βநߏจ "45 Λੜ ! w 1ZUIPO͔Β؆୯ʹ͍͡ΕΔ w 4BGY$MBOHͷ1ZUIPOόΠϯσΟϯάͰ0CKFDUJWF$ͷ
ιʔείʔυΛύʔεͯ͠ΈΔ TBGYEFWCMPHTQPUKQ DMBOHQZUIPOPCKFDUJWFDIUNM python cindex-dump.py sample.h -c -m64 -ObjC DOMπϦʔΈ͍ͨͳͷ
translation_unit objc_interface_decl objc_property_decl objc_class_ref objc_instance_method_decl objc_instance_method_decl parm_decl objc_instance_method_decl parm_decl objc_class_ref
parm_decl sample.h foo hoge NSArray hoge setHoge: hoge barWithArray:limit: array NSArray limit @class NSArray; ! @interface Foo @property NSArray* hoge; - (int)barWithArray:(NSArray*)array limit:(int)limit; @end
ؔ࿈ϓϩδΣΫτ NLSBVU[PCKD ˒ PCKD@NTH4FOEͰ0CKFDUJWF$Λ͑ΔΑ͏ʹ͢Δ(PϥΠϒϥϦ NLSBVU[WBSJBEJD ˒
$ͷՄมҾͷؔΛݺΔΑ͏ʹͳΔ(PϥΠϒϥϦ 3JQ3JQDMBOH@DPNQMFUF ˒ WJNͷิϓϥάΠϯ DMBOHQZUIPOόΠϯσΟϯάར༻ TIJMHBQJSB0CK$SVTU ˒ 3VTUͰJ04ͷϥΠϒϥϦΛ࡞Δαϯϓϧ