--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. 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))
}
--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
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
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)
}
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
--If you can use C language, you can easily write an interface to access I / O.
Recommended Posts