init
This commit is contained in:
commit
8f4f858d86
30 changed files with 9765 additions and 0 deletions
16
examples/classes.gt
Normal file
16
examples/classes.gt
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
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"))
|
||||
}
|
||||
10
examples/hello.gt
Normal file
10
examples/hello.gt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package examples.hello
|
||||
|
||||
fun greet(name: String): String {
|
||||
return "Hello, " + name
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val message = greet("Go backend")
|
||||
println(message)
|
||||
}
|
||||
76
examples/http_server.gt
Normal file
76
examples/http_server.gt
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package examples.http
|
||||
|
||||
import context
|
||||
import fmt
|
||||
import github.com.uptrace.bun
|
||||
import github.com.uptrace.bun.dialect.pgdialect
|
||||
import github.com.uptrace.bun.driver.pgdriver
|
||||
import database.sql
|
||||
import net.http
|
||||
import os
|
||||
|
||||
class BunUser(val Name: String)
|
||||
|
||||
class EpicControllerImpl(val db: *bun.DB) {
|
||||
|
||||
fun hello(w: http.ResponseWriter, r: *http.Request) {
|
||||
fmt.Fprintln(w, "hello from gotlin")
|
||||
}
|
||||
|
||||
fun bunHealth(w: http.ResponseWriter, r: *http.Request) {
|
||||
val ctx = context.Background()
|
||||
db.NewSelect().ColumnExpr("1").Scan(ctx)
|
||||
fmt.Fprintln(w, "bun ok")
|
||||
}
|
||||
|
||||
fun bunUsers(w: http.ResponseWriter, r: *http.Request) {
|
||||
val ctx = context.Background()
|
||||
val model = BunUser("gotlin")
|
||||
|
||||
db.NewCreateTable().Model(model).IfNotExists().Exec(ctx)
|
||||
val user = BunUser("user-from-gotlin")
|
||||
db.NewInsert().Model(user).Exec(ctx)
|
||||
val total = db.NewSelect().Model(model).Count(ctx)
|
||||
|
||||
fmt.Fprintln(w, "bun users total:", total)
|
||||
}
|
||||
}
|
||||
|
||||
worker Counter {
|
||||
val counter = 0
|
||||
|
||||
fun getCount(): Int {
|
||||
return counter
|
||||
}
|
||||
fun increment() {
|
||||
counter += 1
|
||||
println(counter)
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val postgresDsn = "postgresql://postgres:postgres@localhost/postgres?sslmode=disable"
|
||||
val sqlDb = sql.OpenDB(
|
||||
pgdriver.NewConnector(
|
||||
pgdriver.WithDSN(postgresDsn)
|
||||
)
|
||||
)
|
||||
val db = bun.NewDB(sqlDb, pgdialect.New())
|
||||
val counter = Counter()
|
||||
go {
|
||||
while(true) {
|
||||
select {
|
||||
every(1000) -> counter.increment()
|
||||
}
|
||||
}
|
||||
}
|
||||
val epicController = EpicControllerImpl(db)
|
||||
fmt.Println("serving http://localhost:8080")
|
||||
http.HandleFunc("/", epicController.hello)
|
||||
http.HandleFunc("/bun", epicController.bunHealth)
|
||||
http.HandleFunc("/bun/users", epicController.bunUsers)
|
||||
http.HandleFunc("/counter") { w, r ->
|
||||
fmt.Fprintln(w, "bun users total:", counter.getCount())
|
||||
}
|
||||
http.ListenAndServe(":8080", http.DefaultServeMux)
|
||||
}
|
||||
8
examples/imports.gt
Normal file
8
examples/imports.gt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package examples.imports
|
||||
|
||||
import go.strings
|
||||
import go.fmt
|
||||
|
||||
fun main() {
|
||||
fmt.Println(strings.ToUpper("gotlinn"))
|
||||
}
|
||||
9
examples/lambdas.gt
Normal file
9
examples/lambdas.gt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package examples.lambdas
|
||||
|
||||
fun apply(value: String, fn: (String) -> Unit) {
|
||||
fn(value)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
apply("from lambda", { println(it) })
|
||||
}
|
||||
78
examples/showcase.gt
Normal file
78
examples/showcase.gt
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package examples.showcase
|
||||
|
||||
import fmt
|
||||
import strings
|
||||
|
||||
interface Greeter {
|
||||
fun greet(name: String): String
|
||||
}
|
||||
|
||||
class PrefixGreeter(val prefix: String): Greeter {
|
||||
fun greet(name: String): String {
|
||||
return prefix + " " + name
|
||||
}
|
||||
}
|
||||
|
||||
worker Counter {
|
||||
val count = 0
|
||||
|
||||
fun increment() {
|
||||
count += 1
|
||||
}
|
||||
|
||||
fun value(): Int {
|
||||
return count
|
||||
}
|
||||
}
|
||||
|
||||
fun risky(input: String): String {
|
||||
if (input == "boom") {
|
||||
throw "boom requested"
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val greeter: Greeter = PrefixGreeter("hello")
|
||||
println(greeter.greet("gotlin"))
|
||||
|
||||
val names = listOf<String>("ada", "linus")
|
||||
val scores = mapOf<String, Int>("ada", 10, "linus", 8)
|
||||
println(names)
|
||||
println(scores)
|
||||
|
||||
val upper = strings.ToUpper("gotlin")
|
||||
fmt.Println("interop:", upper)
|
||||
|
||||
val result = runCatching({ risky("boom") })
|
||||
if (result.isSuccess()) {
|
||||
println("runCatching: success")
|
||||
} else {
|
||||
println("runCatching:")
|
||||
println(result.exceptionOrNull())
|
||||
}
|
||||
|
||||
val maybe: any = null
|
||||
if (maybe == null) {
|
||||
println("null check works")
|
||||
}
|
||||
|
||||
val counter = Counter()
|
||||
counter.increment()
|
||||
counter.increment()
|
||||
fmt.Println("worker value:", counter.value())
|
||||
|
||||
val ready = Channel<String>()
|
||||
go {
|
||||
select {
|
||||
after(120) -> ready.send("timer fired")
|
||||
}
|
||||
}
|
||||
select {
|
||||
ready -> println("channel says: " + it)
|
||||
}
|
||||
|
||||
select {
|
||||
every(50) -> println("one periodic tick")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue