3 KiB
3 KiB
Gotlin
Gotlin is a small Kotlin-like frontend implemented in Go that targets the Go toolchain.
This is the practical boundary of the prototype:
- It is a Kotlin-flavored language frontend.
- It targets the Go toolchain by generating valid Go source and building through
go build. - It is not a direct integration into Go's internal
cmd/compilebackend APIs.
Supported language slice
fundeclarationsvalandvarInt,String,Boolean,Unit- function types like
(String) -> Unit if,else,while- function calls
- lambdas like
{ x: Int -> println(x) }and{ println(it) } classwith primary-constructor fields and methodsinterfacewith method signaturesprintln(...)- arithmetic, comparison, and boolean operators
Example
package demo
fun fib(n: Int): Int {
if (n < 2) {
return n
}
return fib(n - 1) + fib(n - 2)
}
fun main() {
println(fib(8))
}
Imports from Go packages are supported:
package demo
import strings
fun main() {
println(strings.ToUpper("gotlin"))
}
HTTP server example:
package demo.web
import fmt
import net.http
fun helloHandler(w: http.ResponseWriter, r: *http.Request) {
fmt.Fprintln(w, "hello from gotlin")
}
fun main() {
http.HandleFunc("/", helloHandler)
fmt.Println("serving http://localhost:8080")
http.ListenAndServe(":8080", http.DefaultServeMux)
}
Classes and interfaces:
package demo
interface Greeter {
fun greet(name: String): String
}
class ConsoleGreeter(val prefix: String) {
fun greet(name: String): String {
return prefix + name
}
}
fun main() {
val greeter: Greeter = ConsoleGreeter("hello, ")
println(greeter.greet("gotlin"))
}
go run ./cmd/gotlinc build ./examples/hello.gt
./hello
Emit Go source instead:
go run ./cmd/gotlinc build -src ./examples/hello.gt -o /tmp/hello.go
go run /tmp/hello.go
Run directly:
go run ./cmd/gotlinc run ./examples/hello.gt
Language server:
go build -o ./bin/gotlin-lsp ./cmd/gotlin-lsp
./bin/gotlin-lsp
VS Code extension:
cd ./tools/vscode-gotlin
npm install
npm run build
Notes
valandvarcurrently compile to the same local-variable semantics in Go.- Type inference is local to declarations without an explicit type.
- Top-level declarations currently support functions, classes, and interfaces.
gotlinc buildproduces an executable by default. If-ois omitted, the output name is derived from the input file name.gotlinc build -srcemits Go source instead of a binary.gotlincsupportsbuildandrun, and defaults tobuildif no subcommand is given.- Gotlin source files use the
.gtextension. gotlin-lspprovides diagnostics, hover, and go-to-definition over stdio.gotlin-lspcan optionally usegoplsfor hover and definition on Go-imported symbols.- the VS Code extension adds syntax highlighting, snippets, and launches the LSP for
.gtfiles.