doc.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Gobind generates language bindings that make it possible to call Go
  6. functions from Java and Objective-C.
  7. Typically gobind is not used directly. Instead, a binding is
  8. generated and automatically packaged for Android or iOS by
  9. `gomobile bind`. For more details on installing and using the gomobile
  10. tool, see https://golang.org/x/mobile/cmd/gomobile.
  11. # Binding Go
  12. Gobind generates target language (Java or Objective-C) bindings for
  13. each exported symbol in a Go package. The Go package you choose to
  14. bind defines a cross-language interface.
  15. Bindings require additional Go code be generated, so using gobind
  16. manually requires calling it twice, first with -lang=<target>, where
  17. target is either java or objc, and again with -lang=go. The generated
  18. package can then be _ imported into a Go program, typically built
  19. with -buildmode=c-archive for iOS or -buildmode=c-shared for Android.
  20. These details are handled by the `gomobile bind` command.
  21. # Passing Go objects to target languages
  22. Consider a type for counting:
  23. package mypkg
  24. type Counter struct {
  25. Value int
  26. }
  27. func (c *Counter) Inc() { c.Value++ }
  28. func NewCounter() *Counter { return &Counter{ 5 } }
  29. In Java, the generated bindings are,
  30. public abstract class Mypkg {
  31. public static native Counter newCounter();
  32. }
  33. and
  34. public final class Counter {
  35. public Counter() { ... }
  36. public final long getValue();
  37. public final void setValue(long v);
  38. public void inc();
  39. }
  40. The package-level function newCounter can be called like so:
  41. Counter c = Mypkg.newCounter()
  42. For convenience, functions on the form NewT(...) *T are converted to constructors for T:
  43. Counter c = new Counter()
  44. Both forms returns a Java Counter, which is a proxy for a Go *Counter. Calling the inc, getValue and
  45. setValue methods will call the Go implementations of these methods.
  46. Similarly, the same Go package will generate the Objective-C interface
  47. @class GoMypkgCounter;
  48. @interface GoMypkgCounter : NSObject {
  49. }
  50. @property(strong, readonly) id ref;
  51. - (void)inc;
  52. - (int64_t)value;
  53. - (void)setValue:(int64_t)v;
  54. @end
  55. FOUNDATION_EXPORT GoMypkgCounter* GoMypkgNewCounter(void);
  56. The equivalent of calling newCounter in Go is GoMypkgNewCounter in Objective-C.
  57. The returned GoMypkgCounter* holds a reference to an underlying Go
  58. *Counter.
  59. # Passing target language objects to Go
  60. For a Go interface:
  61. package myfmt
  62. type Printer interface {
  63. Print(s string)
  64. }
  65. func PrintHello(p Printer) {
  66. p.Print("Hello, World!")
  67. }
  68. gobind generates a Java interface that can be used to implement a Printer:
  69. public abstract class Myfmt {
  70. public static void printHello(Printer p0);
  71. }
  72. and
  73. public interface Printer {
  74. public void print(String s);
  75. }
  76. You can implement Printer, and pass it to Go using the printHello
  77. package function:
  78. public class SysPrint implements Printer {
  79. public void print(String s) {
  80. System.out.println(s);
  81. }
  82. }
  83. The Java implementation can be used like so:
  84. Printer printer = new SysPrint();
  85. Myfmt.printHello(printer);
  86. For Objective-C binding, gobind generates a protocol that declares
  87. methods corresponding to Go interface's methods.
  88. @protocol GoMyfmtPrinter
  89. - (void)Print:(NSString*)s;
  90. @end
  91. FOUNDATION_EXPORT void GoMyfmtPrintHello(id<GoMyfmtPrinter> p0);
  92. Any Objective-C classes conforming to the GoMyfmtPrinter protocol can be
  93. passed to Go using the GoMyfmtPrintHello function:
  94. @interface SysPrint : NSObject<GoMyfmtPrinter> {
  95. }
  96. @end
  97. @implementation SysPrint {
  98. }
  99. - (void)Print:(NSString*)s {
  100. NSLog("%@", s);
  101. }
  102. @end
  103. The Objective-C implementation can be used like so:
  104. SysPrint* printer = [[SysPrint alloc] init];
  105. GoMyfmtPrintHello(printer);
  106. # Type restrictions
  107. At present, only a subset of Go types are supported.
  108. All exported symbols in the package must have types that are supported.
  109. Supported types include:
  110. - Signed integer and floating point types.
  111. - String and boolean types.
  112. - Byte slice types. Note that byte slices are passed by reference,
  113. and support mutation.
  114. - Any function type all of whose parameters and results have
  115. supported types. Functions must return either no results,
  116. one result, or two results where the type of the second is
  117. the built-in 'error' type.
  118. - Any interface type, all of whose exported methods have
  119. supported function types.
  120. - Any struct type, all of whose exported methods have
  121. supported function types and all of whose exported fields
  122. have supported types.
  123. Unexported symbols have no effect on the cross-language interface, and
  124. as such are not restricted.
  125. The set of supported types will eventually be expanded to cover more
  126. Go types, but this is a work in progress.
  127. Exceptions and panics are not yet supported. If either pass a language
  128. boundary, the program will exit.
  129. # Reverse bindings
  130. Gobind also supports accessing API from Java or Objective C from Go.
  131. Similar to how Cgo supports the magic "C" import, gobind recognizes
  132. import statements that start with "Java/" or "ObjC/". For example,
  133. to import java.lang.System and call the static method currentTimeMillis:
  134. import "Java/java/lang/System"
  135. t := System.CurrentTimeMillis()
  136. Similarly, to import NSDate and call the static method [NSDate date]:
  137. import "ObjC/Foundation/NSDate"
  138. d := NSDate.Date()
  139. Gobind also supports specifying particular classes, interfaces or
  140. protocols a particular Go struct should extend or implement. For example,
  141. to create an Android Activity subclass MainActivity:
  142. import "Java/android/app/Activity"
  143. type MainActivity struct {
  144. app.Activity
  145. }
  146. Gobind also recognizes Java interfaces as well as Objective C classes and
  147. protocols the same way.
  148. For more details on binding the native API, see the design proposals,
  149. https://golang.org/issues/16876 (Java) and https://golang.org/issues/17102
  150. (Objective C).
  151. # Avoid reference cycles
  152. The language bindings maintain a reference to each object that has been
  153. proxied. When a proxy object becomes unreachable, its finalizer reports
  154. this fact to the object's native side, so that the reference can be
  155. removed, potentially allowing the object to be reclaimed by its native
  156. garbage collector. The mechanism is symmetric.
  157. However, it is possible to create a reference cycle between Go and
  158. objects in target languages, via proxies, meaning objects cannot be
  159. collected. This causes a memory leak.
  160. For example, in Java: if a Go object G holds a reference to the Go
  161. proxy of a Java object J, and J holds a reference to the Java proxy
  162. of G, then the language bindings on each side must keep G and J live
  163. even if they are otherwise unreachable.
  164. We recommend that implementations of foreign interfaces do not hold
  165. references to proxies of objects. That is: if you implement a Go
  166. interface in Java, do not store an instance of Seq.Object inside it.
  167. # Further reading
  168. Examples can be found in http://golang.org/x/mobile/example.
  169. Design doc: http://golang.org/s/gobind
  170. */
  171. package main