From d3b0a93d3d9ae9f0c2e5c9223b7b64f0c52f09c9 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 6 Jun 2022 15:01:24 +0800 Subject: [PATCH] Calling C from Go+ --- README.md | 37 +++++++++++++++++++++++++++++++++--- testdata/helloc2go/README.md | 2 +- testdata/mixgo/README.md | 4 ++-- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fba1f0896..b13cf6a50 100644 --- a/README.md +++ b/README.md @@ -87,11 +87,12 @@ Here is my `Hello world` program: * [Variadic parameters](#variadic-parameters) * [Higher order functions](#higher-order-functions) * [Lambda expressions](#lambda-expressions) +* [Structs](#structs) -* [Structs](#structs) * [Go/Go+ hybrid programming](#gogo-hybrid-programming) +* [Calling C from Go+](#calling-c-from-go) * [Data processing](#data-processing) * [List comprehension](#list-comprehension) * [Select data from a collection](#select-data-from-a-collection) @@ -1046,7 +1047,7 @@ In Go+, we introduce a concept named `auto property`. It is a `get property`, bu ## Go/Go+ hybrid programming -This is an example to show how to mix Go/Go+ programming in the same package. +This is an example to show how to mix Go/Go+ code in the same package. In this example, we have a Go source file named `a.go`: @@ -1073,7 +1074,7 @@ p "world" You can see that Go calls a Go+ function named `sayMix`, and Go+ calls a Go function named `p`. As you are used to in Go programming, this kind of circular reference is allowed. -The output of this example is as follows: +Run `gop run .` to see the output of this example: ``` Mix Go and Go+ @@ -1082,6 +1083,36 @@ Hello, world
⬆ back to toc
+## Calling C from Go+ + +- The `gop c` command (equivalent to the stand-alone `c2go` command) can be used to convert a C project to a Go project. +- `import "C"` and `import "C/xxx"` are used to import a C project converted by c2go. where `import "C"` is short for `import "C/github.com/goplus/libc"`. +- The `C"xxx"` syntax represents C-style string constants. + +Here is [an example to show how Go+ interacts with C](https://github.com/goplus/gop/tree/v1.1/testdata/helloc2go). + +```go +import "C" + +C.printf C"Hello, c2go!\n" +C.fprintf C.stderr, C"Hi, %7.1f\n", 3.14 +``` + +In this example we call two C standard functions `printf` and `fprintf`, passing a C variable `stderr` and two C strings in the form of `C"xxx"` (a Go+ syntax to represent C-style strings). + +Run `gop run .` to see the output of this example: + +``` +Hello, c2go! +Hi, 3.1 +``` + +Of course, the current Go+ support for C is only a preview version, not to the extent that it is actually available in engineering. As far as libc is concerned, the current migration progress is only about 5%, and it is just the beginning. + +In the upcoming Go+ v1.2 version planning, complete support for C is listed as a top priority. Of course, support for cgo and Go templates is also under planning, which is a crucial capability enhancement for Go/Go+ hybrid projects. + +
⬆ back to toc
+ ## Data processing diff --git a/testdata/helloc2go/README.md b/testdata/helloc2go/README.md index a5bdad347..b94b584a6 100644 --- a/testdata/helloc2go/README.md +++ b/testdata/helloc2go/README.md @@ -18,7 +18,7 @@ C.fprintf C.stderr, C"Hi, %7.1f\n", 3.14 In this example we call two C standard functions `printf` and `fprintf`, pass a C variable `stderr` and two C strings in the form of `C"xxx"`. -The output of this example is as follows: +Run `gop run .` to see the output of this example: ``` Hello, c2go! diff --git a/testdata/mixgo/README.md b/testdata/mixgo/README.md index 780dbc07f..12edcc486 100644 --- a/testdata/mixgo/README.md +++ b/testdata/mixgo/README.md @@ -1,4 +1,4 @@ -This is an example to show how to mix Go/Go+ programming in the same package. +This is an example to show how to mix Go/Go+ code in the same package. In this example, we have a Go source file named `a.go`: @@ -25,7 +25,7 @@ p "world" You can see that Go calls a Go+ function named `sayMix`, and Go+ calls a Go function named `p`. As you are used to in Go programming, this kind of circular reference is allowed. -The output of this example is as follows: +Run `gop run .` to see the output of this example: ``` Mix Go and Go+