How to use C++ stl in Golang?

1,303 views
Skip to first unread message

zj huang

unread,
Oct 14, 2014, 3:15:18 AM10/14/14
to golan...@googlegroups.com
I wanna to pass some data just like vector, list or map to Golang
Is it possible except SIWG?

Gustavo Niemeyer

unread,
Oct 14, 2014, 3:19:22 AM10/14/14
to zj huang, golan...@googlegroups.com
It is feasible and not hard, but the C++ functionality needs to be
mapped through a C layer.


On Tue, Oct 14, 2014 at 9:14 AM, zj huang <huangji...@gmail.com> wrote:
> I wanna to pass some data just like vector, list or map to Golang
> Is it possible except SIWG?
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

gustavo @ http://niemeyer.net

Jason E. Aten

unread,
Oct 14, 2014, 3:23:40 AM10/14/14
to golan...@googlegroups.com
On Tuesday, October 14, 2014 12:15:18 AM UTC-7, zj huang wrote:
I wanna to pass some data just like vector, list or map to Golang
Is it possible except SIWG?

You'll have to serialize to a memory buffer from C++, then unserialize those bytes into golang. Many choices of serialization format are available. For starters just write a file to disk before worrying about how to pass the memory around.

You have many choices, but the main three I would look at:

a) encoding/json in the go std lib; very simple to visualize; probably a good place to start. If you need more performance try:
b) Protocol buffers; https://code.google.com/p/gogoprotobuf/  ; if you need even more performance consider
c) Capnproto; https://github.com/glycerine/go-capnproto


zj huang

unread,
Oct 14, 2014, 3:24:37 AM10/14/14
to Gustavo Niemeyer, golan...@googlegroups.com
I appreciate if you can give me some demo code pass a vector to Golang

Gustavo Niemeyer

unread,
Oct 14, 2014, 3:31:39 AM10/14/14
to zj huang, golan...@googlegroups.com
You can see an example of that kind of layering in the qml package:

https://github.com/go-qml/qml

Have a look under cpp/capi.{c,h}, and see how these functions are used from Go.

huangji...@gmail.com

unread,
Oct 14, 2014, 4:39:19 AM10/14/14
to golan...@googlegroups.com, huangji...@gmail.com
I still have no idea how to wrap a vector.
If the return value is int, I can wrap it.
but how to wrap vector to the type Golang support?

在 2014年10月14日星期二UTC+8下午3时31分39秒,Gustavo Niemeyer写道:

Gustavo Niemeyer

unread,
Oct 14, 2014, 4:49:33 AM10/14/14
to zj huang, golan...@googlegroups.com
On Tue, Oct 14, 2014 at 10:39 AM, <huangji...@gmail.com> wrote:
> I still have no idea how to wrap a vector.
> If the return value is int, I can wrap it.
> but how to wrap vector to the type Golang support?

As I mentioned, there are examples of wrapping C++ values in the
project and files that I mentioned, and there are multiple ways of
doing it. If you need to preserve the original identity of the type
and/or mutate it, the idea consists in returning an untyped pointer to
the original C++ value and then statically casting it back into the
proper C++ type when the value re-enters the C/C++ space. If you don't
need to manipulate the original value, you may opt to convert the
whole vector at once to a C structure and hand that to Go instead of
the original vector. That latter approach performs better if you're
just trying to pass data into Go.

The usual memory management caveats apply.


gustavo @ http://niemeyer.net

zj huang

unread,
Oct 14, 2014, 5:26:34 AM10/14/14
to chai2010, Gustavo Niemeyer, golan...@googlegroups.com, j.e....@gmail.com
Thank you so much.
I think I already solve this problem.

but it's so sad that native Golang don't support C++ STL

2014-10-14 17:07 GMT+08:00 chai2010 <chais...@gmail.com>:


2014-10-14 15:14 GMT+08:00 zj huang <huangji...@gmail.com>:
I wanna to pass some data just like vector, list or map to Golang
Is it possible except SIWG?
This is a exampe (can't work on windows, see issue4069):

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chai2010

unread,
Oct 14, 2014, 5:34:42 AM10/14/14
to zj huang, Gustavo Niemeyer, golang-nuts, j.e....@gmail.com
2014-10-14 17:25 GMT+08:00 zj huang <huangji...@gmail.com>:
Thank you so much.
I think I already solve this problem.
This example can work on windows (use shared link):
 

but it's so sad that native Golang don't support C++ STL
cgo only support C, not support C++. 

2014-10-14 17:07 GMT+08:00 chai2010 <chais...@gmail.com>:


2014-10-14 15:14 GMT+08:00 zj huang <huangji...@gmail.com>:
I wanna to pass some data just like vector, list or map to Golang
Is it possible except SIWG?
This is a exampe (can't work on windows, see issue4069):

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Ian Lance Taylor

unread,
Oct 14, 2014, 9:35:53 AM10/14/14
to zj huang, chai2010, Gustavo Niemeyer, golang-nuts, Jason Aten
On Tue, Oct 14, 2014 at 2:25 AM, zj huang <huangji...@gmail.com> wrote:
>
> but it's so sad that native Golang don't support C++ STL

That doesn't really make sense as stated.

SWIG has some support for direct Go access to a C++ vector.

Any such access is going to be somewhat slow, as it requires calling
in between Go and C++ code.

Ian

pww19...@gmail.com

unread,
Apr 21, 2015, 10:09:50 PM4/21/15
to golan...@googlegroups.com
Title:       The core of the core of the big data solutions -- Map
Author:      pengwenwei
Email:       wenwei19710430
Language:    c++
Platform:    Windows, linux
Technology:  Perfect hash algorithm
Level:       Advanced
Description: Map algorithm with high performance
Section      MFC c++ map stl
SubSection   c++ algorithm
License:     (GPLv3)

    Download demo project - 1070 Kb
    Download source - 1070 Kb

Introduction:
For the c++ program, map is used everywhere.And bottleneck of program performance is often the performance of map.Especially in the case of large data,and the business association closely and unable to realize the data distribution and parallel processing condition.So the performance of map becomes the key technology.

In the work experience with telecommunications industry and the information security industry, I was dealing with the big bottom data,especially the most complex information security industry data,all can’t do without map.

For example, IP table, MAC table, telephone number list, domain name resolution table, ID number table query, the Trojan horse virus characteristic code of cloud killing etc..

The map of STL library using binary chop, its has the worst performance.Google Hash map has the optimal performance and memory at present, but it has repeated collision probability.Now the big data rarely use a collision probability map,especially relating to fees, can’t be wrong.

Now I put my algorithms out here,there are three kinds of map,after the build is Hash map.We can test the comparison,my algorithm has the zero probability of collision,but its performance is also better than the hash algorithm, even its ordinary performance has no much difference with Google.

My algorithm is perfect hash algorithm,its key index and the principle of compression algorithm is out of the ordinary,the most important is a completely different structure,so the key index compression  is fundamentally different.The most direct benefit for program is that for the original map need ten servers for solutions but now I only need one server.
Declare: the code can not be used for commercial purposes, if for commercial applications,you can contact me with QQ 75293192.
Download:
https://sourceforge.net/projects/pwwhashmap/files

Applications:
First,modern warfare can’t be without the mass of information query, if the query of enemy target information slows down a second, it could lead to the delaying fighter, leading to failure of the entire war. Information retrieval is inseparable from the map, if military products use pwwhashMap instead of the traditional map,you must be the winner.

Scond,the performance of the router determines the surfing speed, just replace open source router code map for pwwHashMap, its speed can increase ten times.
There are many tables to query and set in the router DHCP ptotocol,such as IP,Mac ,and all these are completed by map.But until now,all map are  using STL liabrary,its performance is very low,and using the Hash map has error probability,so it can only use multi router packet dispersion treatment.If using pwwHashMap, you can save at least ten sets of equipment.

Third,Hadoop is recognized as the big data solutions at present,and its most fundamental thing is super heavy use of the map,instead of SQL and table.Hadoop assumes the huge amounts of data so that the data is completely unable to move, people must carry on the data analysis in the local.But as long as the open source Hadoop code of the map changes into pwwHashMap, the performance will increase hundredfold without any problems.


Background to this article that may be useful such as an introduction to the basic ideas presented:
http://blog.csdn.net/chixinmuzi/article/details/1727195



在 2014年10月14日星期二 UTC+8下午3:15:18,zj huang写道:

pww19...@gmail.com

unread,
Apr 21, 2015, 11:27:46 PM4/21/15
to golan...@googlegroups.com
I wanna to pass some data just like vector, list or map to Golang

pww19...@gmail.com

unread,
Apr 22, 2015, 3:54:01 AM4/22/15
to golan...@googlegroups.com
Title:       The core of the core of the big data solutions -- Map
Author:      pengwenwei
Email:       
I wanna to pass some data just like vector, list or map to Golang

pww19...@gmail.com

unread,
Apr 22, 2015, 3:54:32 AM4/22/15
to golan...@googlegroups.com
I wanna to pass some data just like vector, list or map to Golang

DV

unread,
Apr 22, 2015, 9:13:33 AM4/22/15
to golan...@googlegroups.com
Slap the data into a c array, pass the array to Go, make a slice from it, you're done.
Reply all
Reply to author
Forward
0 new messages