没有很多Go代码可以从中学习语言,我确信我不是唯一一个尝试使用它的人.所以,如果您发现了有关该语言的有趣内容,请在此处发布示例.
我也在找
我正在寻找开发一组C API,它们将围绕我们现有的C++ API来访问我们的核心逻辑(用面向对象的C++编写).这基本上是一个粘合API,允许我们的C++逻辑可以被其他语言使用.什么是一些很好的教程,书籍或最佳实践,介绍了围绕面向对象的C++包装C所涉及的概念?
如何使用Go的"外部函数接口"调出C函数?
常见问题解答中提到了此界面,但我无法在文档的其他地方看到它.
是否可以在cgo中混合一些C++代码?
我试过这个:
package main
/*
#include <iostream>
extern "C" void test(const char* str)
{
std::cout << str;
}
*/
// #cgo CFLAGS: -x c++
// #cgo LDFLAGS: -lstdc++
import "C"
func main() {
C.test(C.CString("Testing!!!"))
}
Run Code Online (Sandbox Code Playgroud)
但我得到这些错误:
error: 'char* CString(_GoString_)' cannot appear in a constant-exp
error: 'void test(const char*)' cannot appear in a constant-expres
error: invalid conversion from 'char* (*)(_GoString_)' to 'long long int' [-fpermissive]
error: invalid conversion from 'void (*)(const char*)' to 'long long int' [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我正在使用go1.0.2和MinGW-w64 4.7.1
go test进行测试。In file included from gofst.cpp:2:
In file included from /usr/local/include/fst/fstlib.h:28:
In file included from /usr/local/include/fst/expanded-fst.h:13:
In file included from /usr/local/include/fst/log.h:26:
/usr/local/include/fst/flags.h:143:50: error: a space is required between consecutive right angle brackets (use '> >')
/usr/local/include/fst/flags.h:174:37: error: a space is required between consecutive right …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个go库,它将充当C库的前端.如果我的一个C结构包含a size_t,我会收到编译错误.AFAIK size_t是一种内置的C型,为什么不去识别呢?
我的头文件看起来像:
typedef struct mystruct
{
char * buffer;
size_t buffer_size;
size_t * length;
} mystruct;
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
gcc failed:
In file included from <stdin>:5:
mydll.h:4: error: expected specifier-qualifier-list before 'size_t'
on input:
typedef struct { char *p; int n; } _GoString_;
_GoString_ GoString(char *p);
char *CString(_GoString_);
#include "mydll.h"
Run Code Online (Sandbox Code Playgroud)
我甚至尝试在之前添加// typedef unsigned long size_t或者// #define size_t unsigned long在.go文件中添加#include,然后我得到"gcc产生无输出".