Skip to content

Commit 95fc349

Browse files
Merge pull request #1 from juanmanuel-tirado/chapter/cgo
Added cgo content.
2 parents 269f6d2 + 688d503 commit 95fc349

File tree

18 files changed

+401
-0
lines changed

18 files changed

+401
-0
lines changed

013_cgo/example_01/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
// #include <stdio.h>
4+
// void func_in_c() {
5+
// printf("printed with C code\n");
6+
// }
7+
import "C"
8+
import "fmt"
9+
10+
func main() {
11+
C.func_in_c()
12+
fmt.Println("printed with Go code")
13+
}

013_cgo/example_02/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
/*
4+
#include <stdio.h>
5+
// This is a comment
6+
void func_in_c() {
7+
printf("printed with C code\n");
8+
}
9+
*/
10+
import "C"
11+
import "fmt"
12+
13+
func main() {
14+
C.func_in_c()
15+
fmt.Println("printed with Go code")
16+
}

013_cgo/example_03/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
/*
4+
int sum(int a, int b) {
5+
return a + b;
6+
}
7+
*/
8+
import "C"
9+
import (
10+
"fmt"
11+
"reflect"
12+
)
13+
14+
func main() {
15+
a := 1
16+
b := 1
17+
c := C.sum(C.int(a), C.int(b))
18+
fmt.Printf("%d + %d = %d\n", a, b, c)
19+
fmt.Println(reflect.TypeOf(c).MethodByName("))
20+
}

013_cgo/example_04/main.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
/*
4+
#include<stdio.h>
5+
void f_char(char in) {
6+
printf("char %c\n", in);
7+
}
8+
void f_schar(signed char in) {
9+
printf("signed char %X\n", in);
10+
}
11+
void f_uchar(unsigned char in) {
12+
printf("unsigned char %c\n", in);
13+
}
14+
void f_short(short in) {
15+
printf("short %d\n", in);
16+
}
17+
void f_ushort(unsigned short in) {
18+
printf("unsigned short %u\n", in);
19+
}
20+
void f_int(int in) {
21+
printf("int %d\n", in);
22+
}
23+
void f_uint(unsigned int in) {
24+
printf("unsigned int %u\n", in);
25+
}
26+
void f_long(long in) {
27+
printf("long %X\n", in);
28+
}
29+
void f_ulong(unsigned long in) {
30+
printf("unsigned long %X\n", in);
31+
}
32+
void f_longlong(long long in) {
33+
printf("long long %X\n", in);
34+
}
35+
void f_ulonglong(unsigned long long in) {
36+
printf("unsigned long long %X\n", in);
37+
}
38+
void f_float(float in) {
39+
printf("float %e\n", in);
40+
}
41+
void f_double(double in) {
42+
printf("float %e\n", in);
43+
}
44+
*/
45+
import "C"
46+
import "math"
47+
48+
func main() {
49+
C.f_char(C.char(42))
50+
C.f_schar(C.schar(-42))
51+
C.f_uchar(C.uchar(42))
52+
53+
C.f_short(C.short(-42))
54+
C.f_ushort(C.ushort(42))
55+
56+
C.f_int(C.int(-42))
57+
C.f_uint(C.uint(math.MaxInt32))
58+
59+
C.f_long(C.long(-42))
60+
C.f_ulong(C.ulong(math.MaxUint64))
61+
62+
C.f_longlong(C.longlong(math.MaxInt64))
63+
C.f_ulonglong(C.ulonglong(math.MaxUint64))
64+
65+
C.f_float(C.float(math.MaxFloat32))
66+
C.f_double(C.double(math.MaxFloat64))
67+
}

013_cgo/example_05/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
/*
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
void func_c_print(char* input) {
7+
printf("C prints: %s\n",input);
8+
}
9+
char* func_c_str() {
10+
char* str = "this is a C string";
11+
return str;
12+
}
13+
*/
14+
import "C"
15+
import (
16+
"fmt"
17+
"unsafe"
18+
)
19+
20+
func main() {
21+
str := "this is a Go string"
22+
goToC := C.CString(str)
23+
C.func_c_print(goToC)
24+
defer C.free(unsafe.Pointer(goToC))
25+
26+
cStr := C.func_c_str()
27+
cToGo := C.GoString(cStr)
28+
fmt.Printf("Go prints: %s\n", cToGo)
29+
30+
nCToGo := C.GoStringN(cStr, 10)
31+
fmt.Printf("Go prints: %s\n", nCToGo)
32+
}

013_cgo/example_06/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
/*
4+
#include<stdio.h>
5+
int sum_array(int a[], int length) {
6+
int i,result = 0;
7+
for (i = 0; i < length; i++) {
8+
result += a[i];
9+
}
10+
return result;
11+
}
12+
*/
13+
import "C"
14+
import "fmt"
15+
16+
func main() {
17+
a := []int32{0, 1, 2, 3, 4}
18+
first := (*C.int)(&a[0])
19+
res := C.sum_array(first, C.int(len(a)))
20+
fmt.Printf("The result is: %d\n", res)
21+
}

013_cgo/example_07/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
/*
4+
#include <stdlib.h>
5+
int* create_array(int n) {
6+
int* result;
7+
result = (int*)malloc(sizeof(int)*n);
8+
for( int i = 0; i < n; i++){
9+
result[i] = i;
10+
}
11+
return result;
12+
}
13+
*/
14+
import "C"
15+
import (
16+
"fmt"
17+
"unsafe"
18+
)
19+
20+
func main() {
21+
array := C.create_array(5)
22+
defer C.free(unsafe.Pointer(array))
23+
fmt.Printf("C returns a pointer: %d\n", array)
24+
aux := (*[5]int32)(unsafe.Pointer(array))
25+
fmt.Printf("We extract the content: %d\n", aux)
26+
for _, i := range aux {
27+
fmt.Printf("->%d\n", i)
28+
}
29+
}

013_cgo/example_08/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
/*
4+
#include <stdio.h>
5+
typedef struct {
6+
int id;
7+
int age;
8+
} User;
9+
10+
void print_user(User *u) {
11+
printf("Id: %d\n", u->id);
12+
printf("Age: %d\n", u->age);
13+
}
14+
*/
15+
import "C"
16+
import "unsafe"
17+
18+
type User struct {
19+
Id int32
20+
Age int32
21+
}
22+
23+
func main() {
24+
u := User{1234, 33}
25+
userToC := (*C.User)(unsafe.Pointer(&u))
26+
C.print_user(userToC)
27+
}

013_cgo/example_09/main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
/*
4+
#include <stdlib.h>
5+
typedef struct {
6+
char* name;
7+
int id;
8+
} User;
9+
10+
User* create_user(char* name) {
11+
User* u;
12+
u = (User*)malloc(sizeof(User*));
13+
u -> name = name;
14+
u -> id = 1234;
15+
return u;
16+
}
17+
*/
18+
import "C"
19+
import (
20+
"fmt"
21+
"unsafe"
22+
)
23+
24+
func main() {
25+
name := C.CString("John")
26+
u := C.create_user(name)
27+
defer C.free(unsafe.Pointer(u))
28+
fmt.Printf("User name: %s\n", C.GoString(u.name))
29+
fmt.Printf("User id: %d\n", u.id)
30+
}

013_cgo/example_10/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
/*
4+
#include <stdlib.h>
5+
typedef struct {
6+
char* name;
7+
int id;
8+
} User;
9+
*/
10+
import "C"
11+
import (
12+
"fmt"
13+
"unsafe"
14+
)
15+
16+
func main() {
17+
name := C.CString("John")
18+
defer C.free(unsafe.Pointer(name))
19+
x := C.User{name: name, id: C.int(1234)}
20+
fmt.Println(C.GoString(x.name), x.id)
21+
// Should we release x?
22+
//C.free(unsafe.Pointer(&x))
23+
}

0 commit comments

Comments
 (0)