init
This commit is contained in:
commit
8f4f858d86
30 changed files with 9765 additions and 0 deletions
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