Just curious - anyway to have a GoLang compiler for KMM?
j
Just curious - anyway to have a GoLang compiler for KMM?
๐Ÿ˜ถ 1
โ“ 1
๐Ÿคจ 3
n
Why did you post this message in #server? The message you have posted has nothing to do with Kotlin.
Go lang isn't related to Kotlin.
j
GoLang is used for server. I will post in multiplatform then
c
??? Golang is a different language than Kotlin. Either can be used for server apps.
j
I am talking about KMM, adding GoLang as an output
n
Unless there is really good reason for using Go lang for server side stuff you are much better off using Kotlin instead.
c
GoLang is a language, not a target platform. What are you looking to accomplish?
๐Ÿ‘ 1
b
Doesn't golang go through llvm as well? I think the OP is looking for golang interop, in which case your best bet is to go through c layer
๐Ÿ‘ 1
Other than that, I wouldn't count on golang as an output for kmp since serverside usecase is already well covered by jvm and native binaries
n
Different compiler infrastructure is used for Go lang. The main distribution of the language isn't LLVM based. Martynas is probably thinking of TinyGo ( https://tinygo.org/ ) which is LLVM based.
No single Kotlin development platform is designed to interop with Go. Kotlin Native is LLVM based but doesn't have the facility to do interop via LLVM Bit Code. Besides, LLVM Bit Code interop doesn't fit nicely into the way Kotlin does programming language interop, which partly involves minimising abstraction and directly interacting with the language as much as possible.
s
graalvm could maybe bridge the gap here @John Huang, because it can output to LLVM. so you can compile your Kotlin and Go targets both to LLVM, in theory, and then call between them via the Polyglot API. See more: https://www.graalvm.org/22.3/reference-manual/llvm/NativeExecution/ https://www.graalvm.org/22.3/reference-manual/llvm/Compiling/ https://www.graalvm.org/22.3/reference-manual/llvm/Interoperability
here's an example calling from Kotlin
Copy code
import java.io.*;
import org.graalvm.polyglot.*;

object Polyglot {
    @JvmStatic fun main(args: Array<String>) {
        val polyglot = Context.newBuilder().allowAllAccess(true).build()
        val file = new File("polyglot")
        val source = Source.newBuilder("llvm", file).build()
        val cpart = polyglot.eval(source)
        cpart.execute()
    }
}
that's calling into native code from kotlin. to call into kotlin, you would need to use the Polyglot C API:
Copy code
#include <stdio.h>
#include <graalvm/llvm/polyglot.h>

int main() {
    void *arrayType = polyglot_java_type("int[]");
    void *array = polyglot_new_instance(arrayType, 4);
    polyglot_set_array_element(array, 2, 24);
    int element = polyglot_as_i32(polyglot_get_array_element(array, 2));
    printf("%d\n", element);
    return element;
}
then call into C from Go, here are some samples for that. https://gist.github.com/tejainece/8b243b56f9f1dadfc501