type A struct {
B struct {
Some string
Len int
}
}
Run Code Online (Sandbox Code Playgroud)
简单的问题.如何初始化这个结构?我想做这样的事情:
a := &A{B:{Some: "xxx", Len: 3}}
Run Code Online (Sandbox Code Playgroud)
预计我会收到一个错误:
missing type in composite literal
Run Code Online (Sandbox Code Playgroud)
当然,我可以创建一个单独的struct B并以这种方式初始化它:
type Btype struct {
Some string
Len int
}
type A struct {
B Btype
}
a := &A{B:Btype{Some: "xxx", Len: 3}}
Run Code Online (Sandbox Code Playgroud)
但它没有第一种方式那么有用.是否有初始化匿名结构的快捷方式?
坚持这个问题.能够只获得通过结构的第一个成员......我做错了什么?将结构从Go传递给C的正确方法是什么?
这是我如何工作的例子:
package main
/*
#include <stdio.h>
typedef struct {
int a;
int b;
} Foo;
void pass_array(Foo **in) {
int i;
for(i = 0; i < 2; i++) {
fprintf(stderr, "[%d, %d]", in[i]->a, in[i]->b);
}
fprintf(stderr, "\n");
}
void pass_struct(Foo *in) {
fprintf(stderr, "[%d, %d]\n", in->a, in->b);
}
*/
import "C"
import (
"unsafe"
)
type Foo struct {
A int
B int
}
func main() {
foo := Foo{25, 26}
foos := []Foo{{25, 26}, {50, 51}}
// wrong …Run Code Online (Sandbox Code Playgroud)