78 lines
1.5 KiB
Text
78 lines
1.5 KiB
Text
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")
|
|
}
|
|
}
|