1 回答

TA貢獻(xiàn)1856條經(jīng)驗 獲得超5個贊
這是Go代碼:
// GetFileSizeC wrapper method for retrieve byte lenght of a file
func GetFileSizeC(filename string) int64 {
// Cast a string to a 'C string'
fname := C.CString(filename)
defer C.free(unsafe.Pointer(fname))
// get the file size of the file
size := C.get_file_size(fname)
return int64(size)
}
從 C
long get_file_size(char *filename) {
long fsize = 0;
FILE *fp;
fp = fopen(filename, "r");
if (fp) {
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
fclose(fp);
}
return fsize;
}
請記住,您需要在 Go 文件中導(dǎo)入之前添加所需的頭文件庫:
package utils
// #cgo CFLAGS: -g -Wall
// #include <stdio.h> |
// #include <stdlib.h> | -> these are the necessary system header
// #include <string.h> |
// #include "cutils.h" <-- this is a custom header file
import "C"
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
....
)
這是一個舊項目,您可以將其用于未來的工作示例:
https://github.com/alessiosavi/GoUtils
- 1 回答
- 0 關(guān)注
- 152 瀏覽
添加回答
舉報