我想使用一些 windows api,但我不知道如何开始。有没有相关的教程?
无论如何,我有一个简单的代码。你能帮我解决这个问题吗?
package mypackage
/*
#cgo LDFLAGS: -luser32
#include <windows.h>
*/
import "C"
import "unsafe"
func MessageBox(m string) {
cm := C.CString(s)
defer C.free(unsafe.Pointer(cm))
C.MessageBoxA(C.HWND(nil), (*C.CHAR)(cm), C.LPCSTR(nil), 0) // It display a message.
}
Run Code Online (Sandbox Code Playgroud)
编辑:我可以处理 char* 但仍然不知道 wchar_t* 是什么。
import "syscall"
func MessageBoxU(m string) {
C.MessageBoxW(C.HWND(nil), (*C.WCHAR)(unsafe.Pointer(syscall.StringToUTF16Ptr(m))), C.LPCWSTR(nil), 0)
}
Run Code Online (Sandbox Code Playgroud)
如果这不是成语,请告诉我。
在Go中使用OpenJtalk,成功包含文件并且引用类型没有问题,但函数会触发undefined reference错误.
jtalk.go:
package main
// #cgo CFLAGS: -I/home/vagrant/open_jtalk/njd [...etc]
/*
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
// Main headers
#include "mecab.h"
#include "njd.h"
#include "jpcommon.h"
#include "HTS_engine.h"
// Sub headers
#include "text2mecab.h"
#include "mecab2njd.h"
#include "njd_set_pronunciation.h"
#include "njd_set_digit.h"
#include "njd_set_accent_phrase.h"
#include "njd_set_accent_type.h"
#include "njd_set_unvoiced_vowel.h"
#include "njd_set_long_vowel.h"
#include "njd2jpcommon.h"
*/
import "C"
type Open_JTalk struct {
mecab C.Mecab each of these struct references are fine
njd C.NJD
jpcommon C.JPCommon
engine C.HTS_Engine
}
func (open_jtalk …Run Code Online (Sandbox Code Playgroud) 我遇到了在线文档与我在访问 GO 代码中的 C 结构的程序中看到的行为之间的脱节。go version说我正在使用:
go version go1.4.2 linux/amd64
Run Code Online (Sandbox Code Playgroud)
根据GO CGO 文档:
在 Go 文件中,作为 Go 中关键字的 C 的结构字段名称可以通过以下划线作为前缀来访问:如果 x 指向具有名为“type”的字段的 C 结构,则 x._type 访问该字段。无法在 Go 中表达的 C 结构字段,例如位字段或未对齐的数据,在 Go 结构中被省略,替换为适当的填充以到达下一个字段或结构的末尾。
我遇到了这个问题,所以做了一个快速的示例程序来测试它:
package main
// struct rec
// {
// int i;
// double d;
// char* s;
// };
import "C"
import "fmt"
func main() {
s := "hello world"
r := C.struct_rec{}
r.i = 9
r.d = 9.876
r.s = C.CString(s)
fmt.Printf("\n\tr.i: %d\n\tr.d: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Go 构建一个示例共享对象库。代码编译(使用命令go build -o libsample.so -buildmode=c-shared .),成功构建了一个共享对象库 - 但是在通过 JNA(来自 Java)或 ctypes(来自 python)访问导出的方法时,我感到恐慌。我用 Go 写的代码是:
// package name: libsample.so
package main
import "C"
import "fmt"
//export Hello
func Hello(s string) {
fmt.Println("Hello " + s + "!")
}
func main() {
}
Run Code Online (Sandbox Code Playgroud)
Hello从 Java访问此方法时:
import com.sun.jna.*;
public class sample {
public interface GoSO extends Library {
GoSO INSTANCE = (GoSO) Native.loadLibrary("sample" ,GoSO.class);
void Hello(String s);
}
public static void main(String[] args) {
GoSO.INSTANCE.Hello("World");
}
}
Run Code Online (Sandbox Code Playgroud)
或来自 …
我可以从 C 调用一个没有参数的 Go 函数,如下所示。这通过编译go build并打印
Hello from Golang main function!
CFunction says: Hello World from CFunction!
Hello from GoFunction!
main.go
package main
//extern int CFunction();
import "C"
import "fmt"
func main() {
fmt.Println("Hello from Golang main function!")
//Calling a CFunction in order to have C call the GoFunction
C.CFunction();
}
//export GoFunction
func GoFunction() {
fmt.Println("Hello from GoFunction!")
}
Run Code Online (Sandbox Code Playgroud)
文件1.c
#include <stdio.h>
#include "_cgo_export.h"
int CFunction() {
char message[] = "Hello World from CFunction!";
printf("CFunction …Run Code Online (Sandbox Code Playgroud) 在 Go 中,由于类型不兼容,我遇到了编译错误,我无法解释。我正在使用该"C"模块。最小示例包含以下 2 个文件:
package module
import "C"
type T struct {
X C.int
}
Run Code Online (Sandbox Code Playgroud)
和一个主程序
package main
import (
"fmt"
"sandbox/module"
)
import "C"
func f() *module.T {
var x C.int = 42
return &module.T{X: x}
}
func main() {
fmt.Printf("value: %d", f().X)
}
Run Code Online (Sandbox Code Playgroud)
这无法编译并显示消息
./main.go:12: cannot use x (type C.int) as type module.C.int in field value。
由于某种原因,编译器认为C.int不等于module.C.int。
它一定与模块有关,C并且代码分布在两个模块中,因为如果我从C.intplain切换到它,它会突然起作用int。
为什么这段代码不能编译?使其编译而不将所有代码压缩在一个模块中的正确解决方案是什么?
我在 Ubuntu 16.04 上使用最新的 Go …
这是我的代码:
helloworld.go :
package main
/*
#include <stdlib.h>
*/
import "C"
import "unsafe"
//export HelloWorld
func HelloWorld() *C.char {
cs := C.CString("Hello World!")
C.free(unsafe.Pointer(cs))
return cs
}
func main() {}
Run Code Online (Sandbox Code Playgroud)
节点helloworld.cc:
#include "helloworld.h"
#include <node.h>
#include <string>
namespace demo {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
args.GetReturnValue().Set(String::NewFromUtf8(isolate, HelloWorld()));
}
void init(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", Method);
}
NODE_MODULE(helloworld, init)
}
Run Code Online (Sandbox Code Playgroud)
当我执行代码时,我得到:
?Oc
或者
??# …
在编写利用网络的程序时,您会发现使用的编译速度明显下降CGO_ENABLED=0。
例如,最简单的HTTP服务器:
package main
import (
"flag"
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi! glad you requested %s.\n", r.URL.Path[1:])
}
func main() {
port := flag.Int("port", 9000, "")
flag.Parse()
http.HandleFunc("/", handler)
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
log.Fatal(err)
}
}
Run Code Online (Sandbox Code Playgroud)
时间是:
% time go build
go build 0.46s user 0.06s system 131% cpu 0.396 total
% time CGO_ENABLED=0 go build
CGO_ENABLED=0 go build 3.93s user 0.15s system 143% cpu …Run Code Online (Sandbox Code Playgroud) 我想使用从 Golang 构建的 WebAssembly 库编写 JS 脚本。但我需要使用C库并通过CGO使用它。
简而言之,我的代码如下所示(只是加载 C 库的示例):
package main
/*
#include <stdlib.h>
*/
import "C"
func main() {
println("Hello")
}
Run Code Online (Sandbox Code Playgroud)
但是当我想使用下面的命令按照教程中的方式构建它时,我遇到了错误。
命令:
GOARCH=wasm GOOS=js go build -o lib.wasm test.go
Run Code Online (Sandbox Code Playgroud)
输出:
can't load package: package main: build constraints exclude all Go files in [Project path]
Run Code Online (Sandbox Code Playgroud)
所以我的问题是是否有可能构建这样的东西。
谢谢。
我在我的 Ubuntu 20.04 操作系统中使用go build -o myApp.
当我在 Ubuntu Server 18.04 上运行此应用程序时,出现此错误:
/lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by ./myApp)
Run Code Online (Sandbox Code Playgroud)
当我在 stackoverflow 中搜索时,有人提到glibc 2.29在服务器上安装。但其他人回答说这种方式有风险,可能会破坏操作系统。
其他人建议使用glibc 2.27.
我怎样才能做到这一点?
cgo ×10
go ×10
c ×1
compilation ×1
glibc ×1
linux ×1
struct ×1
ubuntu ×1
webassembly ×1
windows ×1