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

Merge pull request #436 from quocanh1897/master

Fix error in code block ch2
This commit is contained in:
chai2010 2019-06-26 15:22:56 +08:00 committed by GitHub
commit b3c3423ddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 5 deletions

View File

@ -245,13 +245,13 @@ f, _ := OpenFile("foo.dat")
// 绑定到了 f 对象
// func Close() error
var Close = func Close() error {
var Close = func() error {
return (*File).Close(f)
}
// 绑定到了 f 对象
// func Read(offset int64, data []byte) int
var Read = func Read(offset int64, data []byte) int {
var Read = func(offset int64, data []byte) int {
return (*File).Read(f, offset, data)
}

View File

@ -326,8 +326,10 @@ static char arr[10];
static char *s = "Hello";
*/
import "C"
import "fmt"
import (
"reflect"
"unsafe"
)
func main() {
// 通过 reflect.SliceHeader 转换
var arr0 []byte

View File

@ -162,7 +162,7 @@ func main() {}
CGO的语法细节不在赘述。为了在C语言中使用sum函数我们需要将Go代码编译为一个C静态库
```
$ go build -buildmode=c-archive -o sum.a sum.go
$ go build -buildmode=c-archive -o sum.a main.go
```
如果没有错误的话,以上编译命令将生成一个`sum.a`静态库和`sum.h`头文件。其中`sum.h`头文件将包含sum函数的声明静态库中将包含sum函数的实现。