1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-24 04:22:22 +00:00

ch2-2: 完善字符串内存, 增加Go1.10的变更内容

This commit is contained in:
chai2010 2018-02-18 04:11:09 +08:00
parent c5bbf0d538
commit 0b37329ef9
2 changed files with 17 additions and 1 deletions

View File

@ -90,7 +90,22 @@ extern void helloString(GoString p0);
extern void helloSlice(GoSlice p0); extern void helloSlice(GoSlice p0);
``` ```
不过需要注意的是如果使用了GoString类型则会对`_cgo_export.h`头文件产生依赖而这个头文件是动态输出的。更严谨的做法是为C语言函数接口定义严格的头文件然后基于稳定的头文件实现代码。 不过需要注意的是如果使用了GoString类型则会对`_cgo_export.h`头文件产生依赖,而这个头文件是动态输出的。
Go1.10针对Go字符串增加了一个`_GoString_`预定义类型可以降低在cgo代码中可能对`_cgo_export.h`头文件产生的循环依赖的风险。我们可以调整helloString函数的C语言声明为
```c
extern void helloString(_GoString_ p0);
```
因为`_GoString_`是预定义类型我们无法通过此类型直接访问字符串的长度和指针等信息。Go1.10同时也增加了以下两个函数用于获取字符串结构中的长度和指针信息:
```c
size_t _GoStringLen(_GoString_ s);
const char *_GoStringPtr(_GoString_ s);
```
更严谨的做法是为C语言函数接口定义严格的头文件然后基于稳定的头文件实现代码。
## 结构体、联合、枚举类型 ## 结构体、联合、枚举类型

View File

@ -19,5 +19,6 @@ func main() {
//export SayHello //export SayHello
func SayHello(s string) { func SayHello(s string) {
fmt.Println(int(C._GoStringLen(s)))
fmt.Print(s) fmt.Print(s)
} }