Native Bridge

Package bridge implements the interface between Go, Objective-C and Java. Go code can register functions and objects that be called by ObjC / Java and vice versa. There is an overhead to switching between runtimes, so it is recommended to minimize the number of calls across the barrier.

In each of the below examples we will show two approaches to calling functions. In the first approach, values will be converted to the “destination” runtime, before being passed as function arguments. This is the recommended because of the additional typesafety. In the second approach, function arguments are not transformed, and it is left to the called function to convert the arguments into a useful representation.

Calling ObjC from Go

Registering methods in Objective-C.

@interface ExampleObjcBridge : NSObject
- (NSData *)callWithObjcValues:(NSInteger)param;
- (MatchaGoValue *)callWithGoValues:(MatchaGoValue *)param;
@end

@implementation ExampleObjcBridge
+ (void)load {
    static dispatch_once_t sOnce = 0;
    dispatch_once(&sOnce, ^{
        ObjcBridge *b = [[ObjcBridge alloc] init];
        [[MatchaObjcBridge sharedBridge] setObject:b forKey:@"gomatcha.io/matcha/example"];
    });
}
- (NSString *)callWithForeignValues:(long long)param {
    return [NSString stringWithFormat:@"%lld", param];
}
- (MatchaGoValue *)callWithGoValues:(MatchaGoValue *)param {
    NSString *string = [NSString stringWithFormat:@"%lld", param.toLongLong];
    return [[MatchaGoValue alloc] initWithString:string];
}
@end 

Calling from Go

str = bridge.Bridge("gomatcha.io/matcha/example").Call("callWithForeignValues:", bridge.Int64(456)).ToString()
str2 = bridge.Bridge("gomatcha.io/matcha/example").Call("callWithGoValues:", bridge.Interface(123)).ToInterface().(string)

Calling Java from Go

Registering methods in Java

public class ExampleJavaBridge {
    static {
        ExampleJavaBridge b = new ExampleJavaBridge();
        Bridge.singleton().put("gomatcha.io/matcha/example", b);
    }
    public String callWithJavaValues(Long param) {
        return String.format("Hello %d", param);
    }
    public GoValue callWithGoValues(GoValue param) {
        return new GoValue(String.format("Hello %d", param.toLong()));
    }
}

Calling from Go

str = bridge.Bridge("gomatcha.io/matcha/example").Call("callWithForeignValues", bridge.Int64(456)).ToString()
str2 = bridge.Bridge("gomatcha.io/matcha/example").Call("callWithGoValues", bridge.Interface(123)).ToInterface().(string)

Calling Go from Objc/Java

Registering functions in Go.

func init() {
    bridge.RegisterFunc("gomatcha.io/matcha/examples/bridge callWithGoValues", func(v int64) string {
        return fmt.Sprintf("Call with Go values:%v", v)
    })
    bridge.RegisterFunc("gomatcha.io/matcha/examples/bridge callWithForeignValues", func(v *bridge.Value) *bridge.Value {
        return bridge.String(fmt.Sprintf("Call with Foreign values:%v", v.ToInt64()))
    })
}

Calling from Objective-C.

MatchaGoValue *func1 = [[MatchaGoValue alloc] initWithFunc:@"gomatcha.io/matcha/examples/bridge callWithGoValues"];
NSString *str1 = [func1 call:@"", [[MatchaGoValue alloc] initWithLongLong:123], nil][0].toString;


MatchaGoValue *func2 = [[MatchaGoValue alloc] initWithFunc:@"gomatcha.io/matcha/examples/bridge callWithForeignValues"];
NSString *str2 = (NSString *)[func2 call:@"", [[MatchaGoValue alloc] initWithObject:@456], nil][0].toObject;

Calling from Java.

GoValue func1 = GoValue.withFunc("gomatcha.io/matcha/examples/bridge callWithGoValues");
String str1 = func1.call("", new GoValue(123))[0].toString();

GoValue func2 = GoValue.withFunc("gomatcha.io/matcha/examples/bridge callWithForeignValues");
String str2 = (String)func2.call("", new GoValue(Long.valueOf(123)))[0].toObject();