Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

This talk What is Go? How to use Java library from Go The Go gopher was designed by Renee French. (http://reneefrench.blogspot.com/)

Slide 4

Slide 4 text

What is Go?

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

Simple Low learning cost to start. package main import "fmt" func main() { fmt.Println("hello world") }

Slide 7

Slide 7 text

Tools / go-fmt End of coding style war ● Tab vs Space ● Curly brackets { do something $ gofmt ./… # or use editor plugin

Slide 8

Slide 8 text

Tools / go get Easy to publish and use library/tools No central repository $ go get github.com/juntaki/gogtags $ gogtags -v

Slide 9

Slide 9 text

go-doc Generates documentation from comments and tests godoc.org/github.com/username/repository

Slide 10

Slide 10 text

Go is fun! Let’s use Go

Slide 11

Slide 11 text

How to use Java libraries from Go

Slide 12

Slide 12 text

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...

Slide 13

Slide 13 text

Go can call Java libraries now github.com/juntaki/javago github.com/juntaki/jnigo

Slide 14

Slide 14 text

jnigo Java binding to Go, using JNI Go C Java cgo JNI

Slide 15

Slide 15 text

javago Generate go wrapper code for Java library with jnigo $ javago --classfile java.lang.Math $ ls java java.lang.Math.go

Slide 16

Slide 16 text

Demo

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Conclusion Go is fun!