Structure type in Gox language-GX10.1

Gox in the spring

In the Gox language, the structure type (struct) is actually not very commonly used, because the Gox language itself is a dynamically typed language. Generally, the data similar to the structure can be directly used as an array or mapping type. The data items inside can be of any type. , Including nested composite types. The mapping also supports the general access method of similar member variables using "." (note that in Gox, in order to simplify the analysis of the pointer operation to the interpreter, the members obtained with "." are all value types, and another The operator "@" gets a reference/pointer type).

Therefore, the structure struct in the Gox language is generally compatible with the structure in the Go language, so as to call the existing library functions in the Go language or the member functions of the structure. Please refer to the following example.

// 创建一个Go语言标准库中net/http包内的Client结构/对象
// 并在创建时对其成员Timeout赋以初值15秒
// 由于数据类型的要求,必须强制转换为time.Duration类型
client1T = &net_http.Client{Timeout: time.Duration(15 * time.Second)}

// 这样创建出来的是一个指针/引用
pl("%#v", client1T)

// 用内置函数new也可以创建Go语言中的结构体或对象,获得的也是指针
client2T = new(net_http.Client)

pl("%#v", client2T)

// 然后可以对其字段进行单独赋值
client2T.Timeout = time.Duration(30)

pl("%#v", client2T)

There are detailed comments in this code, and the result is:

λ gox struct.gox
&http.Client{Transport:http.RoundTripper(nil), CheckRedirect:(func(*http.Request, []*http.Request) error)(nil), Jar:http.CookieJar(nil), Timeout:15000000000} 
&http.Client{Transport:http.RoundTripper(nil), CheckRedirect:(func(*http.Request, []*http.Request) error)(nil), Jar:http.CookieJar(nil), Timeout:0}
&http.Client{Transport:http.RoundTripper(nil), CheckRedirect:(func(*http.Request, []*http.Request) error)(nil), Jar:http.CookieJar(nil), Timeout:30}

First of all, there is no way to declare structure directly in Gox language, because as a dynamically typed language, all types cannot be declared, but you can use a method like "a = &MyStruct{}" in Go language to create structure. Or use the make function to directly create a structure type so that you get the value object of the structure, and use the new function to get a pointer (or reference) of the structure type, and then you can use the "." method to reference Member variables in the structure or use "@" to get pointers to member variables.

If another variable is assigned to a future structure variable, it is actually just a reference.

If you need to add methods (ie, member functions) to the structure, the Gox language can provide the concept of object classes, which will be introduced later.

Finally, by the way, the make function can also be used to create some built-in compound types in the Go language, for example:

e = make([]int, len, cap) // 创建一个 int slice,并将长度设置为 len,容量设置为 cap
f = make([][]int, len, cap) // 创建一个 []int 的 slice,并将长度设置为 len,容量设置为 cap

e = make(map[string]int) // 创建一个空的 map[string]int 类型的对象
f = make(map[string]map[string]int) // 创建一个 map[string]map[string]int 类型的对象

ch1 = make(chan bool, 2) // 得到 buffer = 2 的 chan bool
ch2 = make(chan int) // 得到 buffer = 0 的 chan int
ch3 = make(chan map[string]int) // 得到 buffer = 0 的 chan map[string]int


Guess you like

Origin blog.csdn.net/weixin_41462458/article/details/107815519
Recommended