From e6d06cc2787989f019d17ffd841ca8e78be554b2 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 6 May 2024 22:31:33 +0800 Subject: [PATCH] demos: hello, concat --- README.md | 6 ++++-- _demo/concat/concat.go | 21 +++++++++++++++++++++ _demo/genints/gen_ints.go | 18 ++++++++++++++++++ _demo/hello/hello.go | 13 +++++++++++++ _demo/qsort/qsort.go | 8 ++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 _demo/concat/concat.go create mode 100644 _demo/hello/hello.go diff --git a/README.md b/README.md index b4b6e72b..69355b06 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,10 @@ go install -v ./... 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 -* [genints](_demo/genints): closure usage in llgo +* [hello](_demo/hello/hello.go): call C printf to print `Hello world` +* [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 diff --git a/_demo/concat/concat.go b/_demo/concat/concat.go new file mode 100644 index 00000000..84df62c6 --- /dev/null +++ b/_demo/concat/concat.go @@ -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 +*/ diff --git a/_demo/genints/gen_ints.go b/_demo/genints/gen_ints.go index bf8b6d62..e4feeee0 100644 --- a/_demo/genints/gen_ints.go +++ b/_demo/genints/gen_ints.go @@ -43,3 +43,21 @@ func main() { c.Printf(c.Str("%d\n"), v) } } + +/* Posible output: +16807 +282475249 +1622650073 +984943658 +1144108930 +2 +4 +8 +16 +32 +2 +3 +4 +5 +6 +*/ diff --git a/_demo/hello/hello.go b/_demo/hello/hello.go new file mode 100644 index 00000000..7437c3e8 --- /dev/null +++ b/_demo/hello/hello.go @@ -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 +*/ diff --git a/_demo/qsort/qsort.go b/_demo/qsort/qsort.go index 95658371..114bf570 100644 --- a/_demo/qsort/qsort.go +++ b/_demo/qsort/qsort.go @@ -15,3 +15,11 @@ func main() { c.Printf(c.Str("%d\n"), v) } } + +/* Expected output: +2 +7 +8 +23 +100 +*/