我是 go 和 cgo 的新手,在浏览互联网一段时间后,我还没有想出一种从 go 中更改 char* 的好而快速的方法。
从 go& 更改 *C.char 的最快方法是什么
这是我的代码和我尝试更改字符串的尝试(它不起作用)
package asciiEngine
// #include <windows.h>
import "C"
type Screen struct {
Width, Height int
Length C.ulong
Data *C.char
GoData string
HConsole C.HANDLE
BytesWritten C.DWORD
Start C.COORD
}
func (s Screen) Draw() {
C.WriteConsoleOutputCharacter(s.HConsole, s.Data, s.Length, s.Start, &s.BytesWritten)
}
func CreateScreen(width, height int) Screen {
screen := Screen{
Width: width,
Height: height,
Length: C.ulong(width * height),
Data: (*C.char)(C.malloc(C.ulonglong(width * height))),
HConsole: C.CreateConsoleScreenBuffer(C.GENERIC_READ|C.GENERIC_WRITE, 0, nil, C.CONSOLE_TEXTMODE_BUFFER, nil),
BytesWritten: 0,
}
screen.GoData = C.GoString(screen.Data) // my attempt to get a reference to the C string
//C.SetConsoleActiveScreenBuffer(screen.HConsole)
return screen
}
主要去:
package main
// #include "stdio.h"
// void print(char* data) {
// printf(data);
// }
import "C"
import (
"fmt"
"github.com/demantar/ascii-engine"
)
func main() {
screen := asciiEngine.CreateScreen(100, 50)
C.print((*C.char)(screen.Data))
fmt.Println()
screen.GoData = "askdssdfselkkskdkflsekfjdkjfksjeflsdkfjjekdjflskasdfkksdjjekdskdfjkskd"
C.print((*C.char)(screen.Data))
}
输出
P
P
我对 C 也很陌生,我正在这样做,因为我找不到一个库来做这件事
蝴蝶刀刀
相关分类