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

ch2: 增加一个例子

This commit is contained in:
chai2010 2018-01-07 01:07:52 +08:00
parent 0626a52dfb
commit 2febbd7c3c
10 changed files with 285 additions and 0 deletions

1
examples/ch2-xx/hello/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
!/_obj

View File

@ -0,0 +1,2 @@
default:
go tool cgo hello.go

View File

@ -0,0 +1,58 @@
/* Created by cgo - DO NOT EDIT. */
#include <stdlib.h>
#include "_cgo_export.h"
extern void crosscall2(void (*fn)(void *, int, __SIZE_TYPE__), void *, int, __SIZE_TYPE__);
extern __SIZE_TYPE__ _cgo_wait_runtime_init_done();
extern void _cgo_release_context(__SIZE_TYPE__);
extern char* _cgo_topofstack(void);
#define CGO_NO_SANITIZE_THREAD
#define _cgo_tsan_acquire()
#define _cgo_tsan_release()
extern void _cgoexp_16f1900c27a8_helloInt(void *, int, __SIZE_TYPE__);
CGO_NO_SANITIZE_THREAD
void helloInt(GoInt p0)
{
__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();
struct {
GoInt p0;
} __attribute__((__packed__)) a;
a.p0 = p0;
_cgo_tsan_release();
crosscall2(_cgoexp_16f1900c27a8_helloInt, &a, 8, _cgo_ctxt);
_cgo_tsan_acquire();
_cgo_release_context(_cgo_ctxt);
}
extern void _cgoexp_16f1900c27a8_helloString(void *, int, __SIZE_TYPE__);
CGO_NO_SANITIZE_THREAD
void helloString(GoString p0)
{
__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();
struct {
GoString p0;
} __attribute__((__packed__)) a;
a.p0 = p0;
_cgo_tsan_release();
crosscall2(_cgoexp_16f1900c27a8_helloString, &a, 16, _cgo_ctxt);
_cgo_tsan_acquire();
_cgo_release_context(_cgo_ctxt);
}
extern void _cgoexp_16f1900c27a8_helloSlice(void *, int, __SIZE_TYPE__);
CGO_NO_SANITIZE_THREAD
void helloSlice(GoSlice p0)
{
__SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();
struct {
GoSlice p0;
} __attribute__((__packed__)) a;
a.p0 = p0;
_cgo_tsan_release();
crosscall2(_cgoexp_16f1900c27a8_helloSlice, &a, 24, _cgo_ctxt);
_cgo_tsan_acquire();
_cgo_release_context(_cgo_ctxt);
}

View File

@ -0,0 +1,64 @@
/* Created by "go tool cgo" - DO NOT EDIT. */
/* package main */
/* Start of preamble from import "C" comments. */
/* End of preamble from import "C" comments. */
/* Start of boilerplate cgo prologue. */
#line 1 "cgo-gcc-export-header-prolog"
#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
/*
static assertion to make sure the file is being used on architecture
at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
typedef struct { const char *p; GoInt n; } GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
#endif
/* End of boilerplate cgo prologue. */
#ifdef __cplusplus
extern "C" {
#endif
extern void helloInt(GoInt p0);
extern void helloString(GoString p0);
extern void helloSlice(GoSlice p0);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1 @@
_CGO_CFLAGS=

View File

@ -0,0 +1,70 @@
// Created by cgo - DO NOT EDIT
package main
import "unsafe"
import _ "runtime/cgo"
import "syscall"
var _ syscall.Errno
func _Cgo_ptr(ptr unsafe.Pointer) unsafe.Pointer { return ptr }
//go:linkname _Cgo_always_false runtime.cgoAlwaysFalse
var _Cgo_always_false bool
//go:linkname _Cgo_use runtime.cgoUse
func _Cgo_use(interface{})
type _Ctype_void [0]byte
//go:linkname _cgo_runtime_cgocall runtime.cgocall
func _cgo_runtime_cgocall(unsafe.Pointer, uintptr) int32
//go:linkname _cgo_runtime_cgocallback runtime.cgocallback
func _cgo_runtime_cgocallback(unsafe.Pointer, unsafe.Pointer, uintptr, uintptr)
//go:linkname _cgoCheckPointer runtime.cgoCheckPointer
func _cgoCheckPointer(interface{}, ...interface{})
//go:linkname _cgoCheckResult runtime.cgoCheckResult
func _cgoCheckResult(interface{})
//go:cgo_export_dynamic helloInt
//go:linkname _cgoexp_16f1900c27a8_helloInt _cgoexp_16f1900c27a8_helloInt
//go:cgo_export_static _cgoexp_16f1900c27a8_helloInt
//go:nosplit
//go:norace
func _cgoexp_16f1900c27a8_helloInt(a unsafe.Pointer, n int32, ctxt uintptr) {
fn := _cgoexpwrap_16f1900c27a8_helloInt
_cgo_runtime_cgocallback(**(**unsafe.Pointer)(unsafe.Pointer(&fn)), a, uintptr(n), ctxt);
}
func _cgoexpwrap_16f1900c27a8_helloInt(p0 int) {
helloInt(p0)
}
//go:cgo_export_dynamic helloString
//go:linkname _cgoexp_16f1900c27a8_helloString _cgoexp_16f1900c27a8_helloString
//go:cgo_export_static _cgoexp_16f1900c27a8_helloString
//go:nosplit
//go:norace
func _cgoexp_16f1900c27a8_helloString(a unsafe.Pointer, n int32, ctxt uintptr) {
fn := _cgoexpwrap_16f1900c27a8_helloString
_cgo_runtime_cgocallback(**(**unsafe.Pointer)(unsafe.Pointer(&fn)), a, uintptr(n), ctxt);
}
func _cgoexpwrap_16f1900c27a8_helloString(p0 string) {
helloString(p0)
}
//go:cgo_export_dynamic helloSlice
//go:linkname _cgoexp_16f1900c27a8_helloSlice _cgoexp_16f1900c27a8_helloSlice
//go:cgo_export_static _cgoexp_16f1900c27a8_helloSlice
//go:nosplit
//go:norace
func _cgoexp_16f1900c27a8_helloSlice(a unsafe.Pointer, n int32, ctxt uintptr) {
fn := _cgoexpwrap_16f1900c27a8_helloSlice
_cgo_runtime_cgocallback(**(**unsafe.Pointer)(unsafe.Pointer(&fn)), a, uintptr(n), ctxt);
}
func _cgoexpwrap_16f1900c27a8_helloSlice(p0 []byte) {
helloSlice(p0)
}

View File

@ -0,0 +1,12 @@
int main() { return 0; }
void crosscall2(void(*fn)(void*, int, __SIZE_TYPE__), void *a, int c, __SIZE_TYPE__ ctxt) { }
__SIZE_TYPE__ _cgo_wait_runtime_init_done() { return 0; }
void _cgo_release_context(__SIZE_TYPE__ ctxt) { }
char* _cgo_topofstack(void) { return (char*)0; }
void _cgo_allocate(void *a, int c) { }
void _cgo_panic(void *a, int c) { }
void _cgo_reginit(void) { }
#line 1 "cgo-generated-wrappers"
int _cgoexp_16f1900c27a8_helloInt;
int _cgoexp_16f1900c27a8_helloString;
int _cgoexp_16f1900c27a8_helloSlice;

View File

@ -0,0 +1,24 @@
// Created by cgo - DO NOT EDIT
//line /Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-xx/hello/hello.go:1
package main
//line /Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-xx/hello/hello.go:5
func main() {
helloString("hello")
}
//line /Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-xx/hello/hello.go:10
func helloInt(s int) {
println(s)
}
//line /Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-xx/hello/hello.go:15
func helloString(s string) {
println(s)
}
//line /Users/chai/go/src/github.com/chai2010/advanced-go-programming-book/examples/ch2-xx/hello/hello.go:20
func helloSlice(s []byte) {
println(string(s))
}

View File

@ -0,0 +1,31 @@
#line 1 "cgo-gcc-prolog"
/*
If x and y are not equal, the type will be invalid
(have a negative array count) and an inscrutable error will come
out of the compiler and hopefully mention "name".
*/
#define __cgo_compile_assert_eq(x, y, name) typedef char name[(x-y)*(x-y)*-2+1];
/* Check at compile time that the sizes we use match our expectations. */
#define __cgo_size_assert(t, n) __cgo_compile_assert_eq(sizeof(t), n, _cgo_sizeof_##t##_is_not_##n)
__cgo_size_assert(char, 1)
__cgo_size_assert(short, 2)
__cgo_size_assert(int, 4)
typedef long long __cgo_long_long;
__cgo_size_assert(__cgo_long_long, 8)
__cgo_size_assert(float, 4)
__cgo_size_assert(double, 8)
extern char* _cgo_topofstack(void);
#include <errno.h>
#include <string.h>
#define CGO_NO_SANITIZE_THREAD
#define _cgo_tsan_acquire()
#define _cgo_tsan_release()

View File

@ -0,0 +1,22 @@
package main
import "C"
func main() {
helloString("hello") // _GoString_
}
//export helloInt
func helloInt(s int) {
println(s)
}
//export helloString
func helloString(s string) {
println(s)
}
//export helloSlice
func helloSlice(s []byte) {
println(string(s))
}