I tried cgo

About When I'm doing C / C ++, I often wish I could write it in golang. I wrote Hello () in C / C ++ from golang to [cgo](https://golang.org/cmd/ Let's call it with cgo /).

Environment I tried

version
Ubuntu 17.10
gcc 7.2.0
golang 1.8.3

Hello World in C

c_hello.h


#ifndef C_HELLO_H_
#define C_HELLO_H_

void Hello(const char*);

#endif

c_hello.c


#include <stdio.h>
#include "c_hello.h"

void Hello(const char* name) {
  printf("hello %s\n", name);
};

c_hello.go


package main

/*
#cgo LDFLAGS: ./chello.o
#include "chello.h"
*/
import "C"

func main() {
	C.Hello(C.CString("Qiita"))
}

Compile & run

% gcc c_hello.c -c
% go run c_hello.go
% hello Qiita

Hello World in C ++

hello.hpp


#ifndef HELLO_H_
#define HELLO_H_

#ifdef __cplusplus
extern "C" {
#endif

void Hello(const char*);

#ifdef __cplusplus
}
#endif

#endif

hello.cpp


#include <cstdio>
#include "hello.hpp"

void Hello(const char* name) {
  std::printf("hello %s\n", name);
};

hello.go


package main

/*
#cgo LDFLAGS: ./hello.o
#include "hello.hpp"
*/
import "C"

func main() {
	C.Hello(C.CString("Qiita"))
}

Compile & run

% gcc -lstdc++ hello.cpp -c
% go run hello.go
% hello Qiita

Impressions

Recommended Posts

I tried cgo
I tried scraping
I tried PyQ
I tried papermill
I tried Django
I tried spleeter
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried competitive programming
I tried running pymc
I tried ARP spoofing
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried deep learning
I tried AWS CDK!
I tried using Ipython
I tried to debug.
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried using Jupyter
I tried using PyCaret
I tried moving EfficientDet
I tried using Heapq
I tried Python> decorator
I tried Auto Gluon
I tried using folium
I tried using jinja2
I tried AWS Iot
I tried Bayesian optimization!
I tried using folium
I tried using time-window
I tried Value Iteration Networks
I tried scraping with Python
I tried AutoGluon's Image Classification
I tried to learn PredNet
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried clustering with PyCaret
I tried using BigQuery ML
I tried "K-Fold Target Encoding"
I tried to implement PCANet
[I tried] Nand2 Tetris Chapter 6
I tried the changefinder library!
I tried Python C extension
[Python] I tried using OpenPose
I tried to introduce Pylint
I tried Pandas' Sql Upsert