Basic data types and conversion in Gox language-GX5.1

The Gox language uses the Qlang syntax engine by default. The Qlang scripting language is based on the Go language (Golang) with certain improvements. The data types are basically inherited from the Go language, and the data assignment and other operations are basically similar but slightly improved and changed. One major difference is that Gox language uses dynamic typing, and the difference from Go language is mainly reflected in:

  • There is no need to declare the type before the variable assignment. For example, the following are all legal variable assignment methods. When assigning, the Gox language will automatically determine the type; in addition, you can use "=" or ":=", and the effect is the same Yes, ":=" does not mean declaring variables and assigning values ​​as in the Go language. Therefore, it is generally recommended to directly use "=" to assign variables.
a = 1

b = "abc"

  • The same variable can be assigned to different types one after another, which means that in Gox language, the type of the variable is variable, which is obviously different from Go language.
a = 1

a = "list"

a = true

It is possible to assign variable a to integer, string, and boolean in sequence above.

 

Case Sensitive

Note that the Gox language is also case-sensitive, and various identifiers (including variable names, function names, structure names, etc.) are considered different if they have different capitalization.

 

Variable declaration and assignment

In the Gox language, there is no local variable declaration. That is to say, if there is already a global variable a, then the variable with the same name in the function body or embedded code block is considered to be an operation on the global variable. Special attention should be paid to this point , Global variables should be named carefully. It is recommended to increase the G at the end (meaning global) to indicate the difference:

a = 1

fn() {
	a = 2
}()

println(a)

Among them, fn is a keyword for defining functions in Gox language, which is equivalent to func in Go language or function in other languages. println is a built-in function of Gox language, which is used to output information, which is the same as println in other languages. After a defined function, you can add parentheses to call it directly. This is the definition and execution method of an anonymous function. After running this code, you will get the following result:

2

It can be seen that the value of global variable a changed from 1 to 2 after the anonymous function was executed.

The exception is the name of the parameter in the function definition will affect this, the following code,

b = "I'm fine."

fn(b) {
	b = "Hi!"
}(b)

println(b)

The execution result is:

I'm fine.

It can be clearly seen that since the anonymous function defined in this code requires a parameter and the name is the same as the name of the global variable b, in this function, the operation on the variable b is based on the value of the variable passed into the function. For the operation, we know that for the parameter passed by value, operating it in the function will not affect the original variable itself, so the value of the global variable b output after the function is executed remains unchanged.

In addition, note that for the calling method of an anonymous function with parameters, the required parameters should be brought in the following parentheses, and the b in the function definition of fn and the b entered in the calling function represent two concepts, the first It is the formal parameter of the function. The second one refers to the global variable b. Don't confuse it.

Boolean type

Boolean type is generally called boolean in English, also often referred to as bool. The value of Boolean type data is only true and false, which represent the opposite concepts of true and false, yes or no, respectively. Boolean values ​​or variables are often used in conditional judgment branch code.

b = true

printf("[%T] %v\n", b, b)

c = false

printf("[%T] %v\n", c, c)

printf("!b = %v\n", !b)

printf("b == c: %v\n", b == c)

printf("1 > 14: %v\n", 1 > 14)

printf("b == true: %v\n", b == true)

printf("b && c: %v\n", b && c)

printf("b || c: %v\n", b || c)

In the above code, the printf function is also a built-in function of the Gox language, which is basically the same as the printf function of other languages, and is exactly the same as the printf function in the Go language. Therefore, if you want to output a carriage return and line feed, you must also use the escape character "\n".

After this code is executed, the output is as follows:

λ gox -gopath test
[bool] true
[bool] false
!b = false
b == c: false
1 > 14: false
b == true: true
b && c: false
b || c: true

Among them, it can be seen that the value of variable a is true, the value of variable b is false, "!" is the negation (also called "not") operator, if it is false, the negation is true, and the negation of true is false. The calculation result of many expressions is also bool type, such as "bc" means judging whether the values ​​in variable b and c are equal ("The symbol indicates whether the variable values ​​on both sides are equal), and the result is also a bool type value. Since the values ​​in b and c are not equal, the calculation result of "b == c" is false. Therefore, b = The result of the expression = true is true, because the value of b is true. "&&" and "||" mean "logical AND" and "logical OR", which is consistent with many other languages. True && true is still true, true && false is false, false && true or false, false && false is also false; and true || true is true, true || false or false, false || true is also true, false || false is false.

Integer type

c1 = 19

c2 = 18

println(c1 + c2/3)

printfln("%T, %v", c1, c1)

printfln("%T", c1+c2)
printfln("%T", c2/3)
printfln("%T", c1+c2/3)
printfln("%T, %v", (c1+c2/3)*6, (c1+c2/3)*6)

c1++
c1 *= 3

c2 += 5
c2--

printfln("c1: %v, c2: %v, %T", c1, c2, c1)

The execution result of this code is:

λ gox -gopath test
25
int, 19
int
int
int
int, 150
c1: 60, c2: 22, int

It can be clearly seen that the result of integer operations is generally a certificate. And the integers are all 64 bits. In addition, the operators ++, -, *=, += are also valid (and -= and /= can also be used).

In addition, printfln is a built-in function of the Gox language, which is equivalent to the printf function and will output an extra carriage return at the end. And "%T" and "%v" are format characters inherited from Go language, "%T" represents the type of the corresponding variable after the output, and "%v" is the string representation of the output of any variable.

The Gox language also supports int64, uint8, rune and other common types in the Go language, which will be used in some function calls, and can be converted in the following ways when needed:

c1 = 19
c2 = int64(c1)

 

Floating point type

f1 = 1.32

previus_f1 = f1

f1 = f1 * 0.8

print(previus_f1, "*", 0.8, "=", f1)
println()

f2 = 0.99
f2 /= 0.3

print(0.99, "/", 0.3, "=", f2, "\n")

The execution result is:

λ gox -gopath test
1.32*0.8=1.056
0.99/0.3=3.3000000000000003

It should be noted that print is also a built-in function of the Gox language. The function is similar to print in other languages. If you want to output carriage return and linefeed characters, you can use the two methods shown in this code, one is to add one without parameters The println function, the other is to output one more escape character "\n".

 

String type

Here are some examples of string assignment and operation:

s1 = "abc"

s2 = s1 + "3"

pv("s2")

println(s1, "+", "3", "=", s2)

s5 = "上善若水"

pv("s5")

s6 = []byte(s5)

println(s6)

t = rune(5)
pv("t")

s7 = []rune("上善若水")

pv("s7")

pl("s5[1:2] = %#v", s5[1:2])

pl("s6[1:2] = %#v", s6[1:2])

pl("s7[1:2] = %#v", s7[1:2])

pl("string(s7[1:3]) = %#v", string(s7[1:3]))

pl("string([]byte(string(s7[1:3]))) = %#v", string([]byte(string(s7[1:3]))))

pl("%c", s5[1])
pl("%c", s6[1])
pl("%c", s7[1])

pl("%T, %#v", s5[1], s5[1])
pl("%T, %#v", s6[1], s6[1])
pl("%T, %#v", s7[1], s7[1])

for i = 0; i < len(s5); i++ {
	pl("%v: %v", i, s5[i])
}

for v = range s7 {
	pl("%#T, %v", byte(v), byte(v))
}

The result of the operation is:

λ gox -gopath test                                          
s2(string): abc3                                            
abc + 3 = abc3                                              
s5(string): 上善若水                                            
[228 184 138 229 150 132 232 139 165 230 176 180]           
t(int32): 5                                                 
s7([]int32): [19978 21892 33509 27700]                      
s5[1:2] = "\xb8"                                            
s6[1:2] = []byte{0xb8}                                      
s7[1:2] = []int32{21892}                                    
string(s7[1:3]) = "善若"                                      
string([]byte(string(s7[1:3]))) = "善若"                      
¸                                                           
¸                                                           
善                                                           
int, 184                                                    
int, 184                                                    
int, 21892                                                  
0: 228                                                      
1: 184                                                      
2: 138                                                      
3: 229                                                      
4: 150                                                      
5: 132                                                      
6: 232                                                      
7: 139                                                      
8: 165                                                      
9: 230                                                      
10: 176                                                     
11: 180                                                     
int, 0                                                      
int, 1                                                      
int, 2                                                      
int, 3                                                      

The things to note here are:

  • The pv function can view the name, type and value of the variable, which is more convenient when debugging the code, but note that the parameter of the function requires the input of the variable name, which is a string, and quotes;
  • The pl function is the same as printfln, just a shorthand;
  • The meaning of string addition is to concatenate (or merge) two strings;
  • Like the Go language, for UTF-8 encoded characters, they can be processed correctly by converting them into []rune type. Therefore, to traverse utf-8 strings correctly, you need to convert them to []rune type (rune type is essentially int32 type);

 

Empty type nil

The empty types in the Gox language are not actually used in so many scenarios. The main uses are as follows:

  • It is used to declare a variable in advance, and the type or value may not be determined during the declaration; for example, when defining a global variable assigned later; but in fact, for this purpose, the Gox language can directly declare a variable with a variable name;
  • Used for interaction and judgment when calling functions in the Go language library that return nil, such as common error types;

See the following example:

pv("aaa")

println(aaa)

aaa = 18

pv("aaa")

println("aaa")

b = nil

pv("b")

println(b)

println("------")

c, errT = tk.StrToInt("12ab")

if errT != nil {
	println("Error:", errT.Error())
}

pv("c")

pv("errT")

c, errT = tk.StrToInt("123")

pv("c")

pv("errT")

if errT != nil {
	println("Error:", errT.Error())
}

The execution result is:

aaa(spec.undefinedType): undefined
undefined
aaa(int): 18
aaa
b(<nil>): <nil>
<nil>
------
Error: strconv.ParseInt: parsing "12ab": invalid syntax
c(int): 0
errT(*strconv.NumError): strconv.ParseInt: parsing "12ab": invalid syntax
c(int): 123
errT(<nil>): <nil>

The tk.StrToInt function in the code is a function that returns the error type. When the string passed in the parameter cannot be converted to an integer type, the second value (error type) returned will not be nil but can be converted normally. Will return a nil value.

Also note that in Gox language, if a variable is directly referenced (not assigned) without defining a variable, it will get the value undefined, which is somewhat similar to the Javascript language. In Go language, such usage will produce panic. Of course, it can also be used for conditional judgment

if a == undefined {…}

To judge. For arrays and maps (map), undefined is also used to determine whether there is a required index value and key value, for example:

m = map[string]string{"a": "1", "b": "ABC"}

v1 = m["a"]
pl("v1: %#v", v1)

v2 = m.a
pl("v2: %#v", v2)

v3 = m["c"]
pl("v3: %#v", v3)

if v3 == undefined {
	pln("v3 is undefined")
}

The result of the operation is:

λ gox -gopath test
v1: "1"
v2: "1"
v3: 0
v3 is undefined

You can see how to use undefined and the displayed value when it is output.

 

Data type conversion

In the Gox language, you can use int(a), string(s), etc. to perform data type conversion in a similar way to the Go language. For some types that cannot be directly converted, you need to use some other functions to perform the conversion action. In addition, you can use the built-in function type to get the type of a variable. The following example code demonstrates these usages.

a = 1
b = int64(2)

println("type of a is:", type(a))
println("type of b is:", type(b))

println("a + b =", a+b)
printfln("a + b = %#v", a+b)

a1 = tk.IntToStr(a)
b1 = tk.IntToStr(b)

printfln("type of a1 is: %T", a1)
printfln("value of a1 is: %v", a1)
printfln("internal value of a1 is: %#v", a1)

println("a1 + b1 =", a1+b1)
printfln("a1 + b1 = %#v", a1+b1)

a2 = tk.StrToFloat64WithDefaultValue(a1, 0)
b2 = tk.StrToFloat64WithDefaultValue(b1, 0)

printfln("a2 + b2 = %#v", a2+b2)
printfln("type of a2 + b2 is: %T", a2+b2)

The running result is:

λ gox -gopath test
type of a is: int
type of b is: int64
a + b = 3
a + b = 3
type of a1 is: string
value of a1 is: 1
internal value of a1 is: "1"
a1 + b1 = 12
a1 + b1 = "12"
a2 + b2 = 3
type of a2 + b2 is: float64

Guess you like

Origin blog.csdn.net/weixin_41462458/article/details/107614359