Go language to see and remember Part 7 C language in GO language

Let's write C language in GO language.

Preparation: Install GCC.

--For Windows 10 64bit -[mingw-w64-install.exe](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe / download) will be installed. image.png Select ** x86_64 **. The first step is the usual ** Hello World **

cPrint.go


package main

// #include <stdio.h>
// #include <stdlib.h>
//
// static void cprint(char* s) {
//   printf("%s\n", s);
// }
import "C"
import "unsafe"

func main() {
	cs := C.CString("Hello World C")
	C.cprint(cs)
	C.free(unsafe.Pointer(cs))
}

//のコメント部分にC言語でコーディングします。

--The above example defines a cprint function and calls printf. --Calling the C free function. --Include the required above

1 stdio.h printf 2 stdlib.h free I am doing it at.

>go run cprint.go
Hello World C

Pass an array to get the sum. (int *) version

sum.go


package main

// int cal_sum(int *x,int n) {
// int sum=0;
//	for(int i=0;i<n;i++){
// 		sum+=x[i];
//  }
// return sum;
//}
import "C"
import "fmt"

func main() {
	x := []C.int{1, 2, 3, 4,5,6,7,8,9,10}
	a:=C.cal_sum((*C.int)(&x[0]),(C.int)(len(x)))
	fmt.Println("sigma x=",a)
}

>go run sum.go
sigma x= 55

Pass an array to get the sum. (float *) version

sigma.go


package main

// float cal_sum(float *x,int n) {
// float sum=0.0;
//	for(int i=0;i<n;i++){
// 		sum+=x[i];
//  }
// return sum;
//}
import "C"
import "fmt"

func main() {
	x := []C.float{10.0, 20.0, 30.0, 40.0,50.0,60.0,70.0,80.0,90.0,100.0}
	a:=C.cal_sum((*C.float)(&x[0]),(C.int)(len(x)))
	fmt.Println("sigma x=",a)
}

Pass an array to find the average. (float *) version

avr.go


package main

// float cal_sum(float *x,int n) {
// float sum=0.0;
//	for(int i=0;i<n;i++){
// 		sum+=x[i];
//  }
// return sum/n;
//}
import "C"
import "fmt"

func main() {
	x := []C.float{10.0, 20.0, 30.0, 40.0,50.0,60.0,70.0,80.0,90.0,100.0}
	a:=C.cal_sum((*C.float)(&x[0]),(C.int)(len(x)))
	fmt.Println("avr a=",a)
}

>go run avr.go
avr a= 55

CGO is a very convenient mechanism.

--If you can use C language, you can easily write an interface to access I / O.

Recommended Posts

Go language to see and remember Part 7 C language in GO language
Go language to see and remember Part 8 Call GO language from Python
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Post to slack in Go language
I tried to illustrate the time and time in C language
How to multi-process exclusive control in C language
Try to make a Python module in C language
The story of porting code from C to Go and getting hooked (and to the language spec)
Machine language embedding in C language
Heapsort made in C language
Hello World in GO language
How to create and use static / dynamic libraries in C
Read json in C # and convert to dictionary type (forced)
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Introduction to Socket API Learned in C Part 2 Client Edition
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Introduction to Socket API Learned in C Part 3 TCP Server / Client # 1
Try implementing Yubaba in Go language
Multi-instance module test in C language
Created a package to support AWS Lambda development in Go language
Use optinal type-like in Go language
Function pointer and objdump ~ C language ~
Realize interface class in C language
Introduction to Protobuf-c (C language ⇔ Python)
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
How to wrap C in Python
Think seriously about what language to use in programming education and programming education.
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
Write tests in GO language + gin
Segfault with 16 characters in C language
Introduction to Socket API Learned in C Part 4 UDP Server / Client # 1
Do something object-oriented in GO language
[C language] I want to generate random numbers in the specified range
How to make a request to bitFlyer Lightning's Private API in Go language
Linked list (list_head / queue) in C language
Implement and understand union-find trees in Go
Implement part of the process in C ++
How to use Google Test in C
To win in Forex Part 4 ~ LINE Notification
Write O_SYNC file in C and Python
Socket communication by C language and Python
Generate C language from S-expressions in Python
How to use is and == in Python
[Golang] About Go language Producer and Consumer
Function to extract the maximum and minimum values ​​in a slice with Go
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)
I made a module in C language to filter images loaded by Python
A tool to insert the country name and country code in the IP address part
How to use the C library in Python
Java programmer touched Go language (Implement Java inheritance in Go language)
Implemented memoization recursion and exploration in Python and Go
Writing logs to CSV file (Python, C language)
Send messages to Skype and Chatwork in Python
Set up a UDP server in C language