Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Go to call Java libraries

Using Go to call Java libraries

De059f612e2f68589831e4bde0f15c83?s=128

Jumpei Takiyasu

October 13, 2017
Tweet

More Decks by Jumpei Takiyasu

Other Decks in Technology

Transcript

  1. Using Go to call Java libraries Jumpei Takiyasu M3, Inc.

  2. Me Jumpei Takiyasu M3, Inc. Github/Twitter: @juntaki https://juntaki.com

  3. This talk What is Go? How to use Java library

    from Go The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)
  4. What is Go?

  5. Go • Simple language spec. • Official development tools •

    Statically typed • Compile to native code • Concurrency etc.. Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
  6. Simple Low learning cost to start. package main import "fmt"

    func main() { fmt.Println("hello world") }
  7. Tools / go-fmt End of coding style war • Tab

    vs Space • Curly brackets { do something $ gofmt ./… # or use editor plugin
  8. Tools / go get Easy to publish and use library/tools

    No central repository $ go get github.com/juntaki/gogtags $ gogtags -v
  9. go-doc Generates documentation from comments and tests godoc.org/github.com/username/repository

  10. Go is fun! Let’s use Go

  11. How to use Java libraries from Go

  12. Motivation My team is using Kotlin - Why? Statically typed

    ʕ ◔ϖ◔ʔ < OK “Kotlin” sounds cute (for Japanese) ʕ ◔ϖ◔ʔ < cute Kotlin can be used with our Java libraries ʕ - ϖ - ʔ < mmm...
  13. Go can call Java libraries now github.com/juntaki/javago github.com/juntaki/jnigo

  14. jnigo Java binding to Go, using JNI Go C Java

    cgo JNI
  15. javago Generate go wrapper code for Java library with jnigo

    $ javago --classfile java.lang.Math $ ls java java.lang.Math.go
  16. Demo

  17. Garbage collection, Memory management Java and C things are wrapped

    by Go object. GoObject C (JNI) Java GetObject / malloc GetObject Object reference Pointer to Object ref. NewGlobalRef Go GC runtime.Finalizer DeleteGlobalRef free Java GC
  18. Method overloading func Mathmax(args ...interface{}) (jnigo.JObject, error) { convertedArgs, err

    := jvm.ConvertAll(args) if err != nil { return nil, err } sigArgs := "" for _, arg := range convertedArgs { sigArgs += arg.Signature() } sigMap := map[string]string{"JJ":"(JJ)J", "FF":"(FF)F", "DD":"(DD)D", "II":"(II)I"} return jvm.CallStaticFunction("java/lang/Math", "max", sigMap[sigArgs], convertedArgs) } Simulated by reflection. Switch function signature by argument signatures
  19. Conclusion Go is fun!