demos: hello, concat

This commit is contained in:
xushiwei
2024-05-06 22:31:33 +08:00
parent c373a5b505
commit e6d06cc278
5 changed files with 64 additions and 2 deletions

View File

@@ -36,8 +36,10 @@ go install -v ./...
The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it): The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it):
* [qsort](_demo/qsort): call C function with callback * [hello](_demo/hello/hello.go): call C printf to print `Hello world`
* [genints](_demo/genints): closure usage in llgo * [concat](_demo/concat/concat.go): call C fprintf with stderr, and Go variable parameter function
* [qsort](_demo/qsort/qsort.go): call C function with a callback (eg. qsort)
* [genints](_demo/genints/genints.go): various forms of closure usage (including C function, recv.method and anonymous function)
### How to run demos ### How to run demos

21
_demo/concat/concat.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"github.com/goplus/llgo/c"
)
func concat(args ...string) (ret string) {
for _, v := range args {
ret += v
}
return
}
func main() {
result := concat("Hello", " ", "World")
c.Fprintf(c.Stderr, c.Str("Hi, %s\n"), c.AllocaCStr(result))
}
/* Expected output (stderr):
Hi, Hello World
*/

View File

@@ -43,3 +43,21 @@ func main() {
c.Printf(c.Str("%d\n"), v) c.Printf(c.Str("%d\n"), v)
} }
} }
/* Posible output:
16807
282475249
1622650073
984943658
1144108930
2
4
8
16
32
2
3
4
5
6
*/

13
_demo/hello/hello.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"github.com/goplus/llgo/c"
)
func main() {
c.Printf(c.Str("Hello world\n"))
}
/* Expected output:
Hello World
*/

View File

@@ -15,3 +15,11 @@ func main() {
c.Printf(c.Str("%d\n"), v) c.Printf(c.Str("%d\n"), v)
} }
} }
/* Expected output:
2
7
8
23
100
*/