• Golang Getting started (1) : Install and configure the meaning of environment variables
  • Golang Primer (2) : Learn the basic grammar of GO in one day
  • Golang Primer (3) : Learn advanced GO grammar in one day
  • Golang 101 (4) : Concurrency

Abstract

Once you’ve configured the environment, it’s all about the syntax of the language. In this article, the author hopes to briefly introduce the various syntax of Golang, and make some simple comparisons with C and Java to deepen memory. Since this is only the second article to introduce Golang, this article does not delve into some of the instructions. It just stays at the level of “how to use”. As for “why”, it involves specific application scenarios and assembly instructions, which the author will introduce in a later article.

1 guide package

It is well known that “Hello World” is a sense of ritual for programmers.

And this line, “Hello World,” is bound to refer to input and output methods. Therefore, how to import packages is the first step we need to study.

In C, we use include, and in Java, we use import. In Golang, too, we use import to import other packages. In the previous article, we mentioned that for imported packages, the compiler looks first in GOROOT, then in the project-specific GOPATH, and finally in the global GOPATH. If none is found, the compiler will report an error.

Note that one big difference between Golang and Java is that in Golang, import imports the directory, not the package name. Also, Golang does not mandate that package names and directory names be the same.

Here are some examples to illustrate the relationship between package names and directories in Golang, starting with the directory structure:



src



test1.go

package pktest

func Func1()  {
	println("This is the first function.")}Copy the code

Test2. Go as follows:

package pktest

func Func2()  {
	println("This is the second function.")}Copy the code

Let’s look at testmain.go:

package main

import "package1/package2"

func main() {
	pktest.Func1()
}
Copy the code

Notice that we called Func1 with pktest instead of package2 as we thought it would be in Package1 /package2.

Func1 (package2.func1); Func1 (test1.func1);

This is because in Golang, it is not mandatory that package names and directory names be the same. That is, in the example above, the folder name in the path we refer to is Package2, while the two files below this folder, their package names, are set to PKtest. In the Golang reference, we need to fill in the relative path of the source file.

The packageName and path are two different concepts. The file name is not explicitly referenced in Golang. The usual reference format is packageName.

The conclusions are as follows:

  • importYou import the relative path of the source file, not the package name.
  • It is customary, but not mandatory, to ensure that the package name and directory name are the same (but not recommended, as it is easy for the person calling the package to not quickly know what the package name is)
  • Use the package name instead of the directory name when referring to members within the package in your code.
  • Only one package name can exist in a folder, and there are no other restrictions on the name of the source file.
  • If multiple folders have packages with the same name, they are actually independent packages.

The above is excerpted from this article

2 the statement

Now that we’ve looked at the package guide, let’s look at how to declare a variable. There are also major differences between C and Java in declaring variables.

2.1 Definition of variables

Let’s define some variables:

var a int
var b float32
var c, d float64
e, f := 9, 10
var g = "Ricardo"
Copy the code

As you can see, to define a variable in Golang, you use the var keyword, and unlike in C or Java, you write the type of the variable after the variable name. Not only that, Golang allows us to define multiple variables at once and assign them simultaneously.

Another way to do this is to use the symbol :=. With this notation, the developer no longer needs to write the var keyword, just define the variable name and assign the value after it. Also, the Golang compiler automatically deduces the type of the variable based on the type of the value that follows.

During the definition of a variable, if the initial value of the variable is assigned during the definition, there is no need to declare the type of the variable, such as variable G.

Note that Golang is a strongly typed language; all variables must have types, and variables can only store certain types of data.

2.2 Anonymous Variables

A variable whose identifier is _ (underscore) is an anonymous variable reserved by the system. It is released immediately after being assigned a value. It is used as a variable placeholder to assign value structures to its variables. Usually used for batch assignment. For example, if a function returns multiple values and we only need some of them, we do not need to use _ as a placeholder

func main() {// Call the function, just need the second return value, first, Func getData() (int, int, int) {// getData() (int, int, int)return2, 4, 8}Copy the code

As the code above shows, if I only need the value of a variable, there is no need to define additional meaningless variable names, just placeholder anonymous variables.

2.3 constant

In Golang’s constant definition, the const keyword is used and the := identifier cannot be used.

3 determine

When we use Java or C, we write a statement like this:

if(condition){
    ...
}
Copy the code

In Golang, the only difference is that curly braces are not required, but curly braces are still required. As follows:

func pow(x, n, lim float64).float64 {
	if v := math.Pow(x, n); v < lim {
	    return v
	}
	return lim
}
Copy the code

In addition to not having to write curly braces, Golang allows you to execute a simple statement with a semicolon before judging a condition; Separated.

4 cycle

In Golang, there is only one loop, the for loop.

As with judgment statements, there are no parentheses in Golang.

func main() {
	sum := 0
	for i := 0; i < 10; i++ {
		sum += i
	}
	fmt.Println(sum)
}
Copy the code

In addition, initialization and postposition statements are optional in loop conditions, and when the semicolon is removed, the for loop becomes a while loop.

func main() {
	sum := 1
	for sum < 1000 {
		sum += sum
	}
	fmt.Println(sum)
}
Copy the code

Not only that, but if you omit the loop condition, the loop will never end, so an infinite loop can be written very tightly, which, in this case, has the same effect as while(true).

func main() {
	for{... }}Copy the code

5 function

5.1 Definition of functions

In Golang’s function definition, all functions begin with func, and Golang recommends using the hump nomenclature.

Note that in Golang’s function, if the first letter is lowercase, it can only be used inside the package; If the initial letter is uppercase, it can be used outside the package. Use lowercase functions, ‘private’, use uppercase functions, ‘public’.

In Golang’s function definition, you can also take no arguments, or multiple arguments. In the process of parameter definition, the variable name is defined first and then the variable type is declared according to the format of variable definition. For the return type of a function, write the function name first, then the return type:

func add(x int, y int) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}
Copy the code

In addition, for two parameters of the same type, you can write only one parameter type, as follows:

func add(x, y int) int {
	return x + y
}
Copy the code

In Golang, the return value of a function is different from that of C or Java.

Functions in Golang can return any number of return values.

For example, here’s Leo,

func swap(x, y string) (string, string) {
	return y, x
}

func main() {
	a, b := swap("hello"."world")
	fmt.Println(a, b)
}
Copy the code

Second, the return value of a function can be named:

func split(sum int) (x, y int) {
	x = sum * 4 / 9
	y = sum - x
	return
}
Copy the code

In this case, we can understand that these variable values are pre-defined at the top of the function, and the empty return statement defaults to returning all the defined return variables.

5.2 the defer

In Golang, there is a keyword called defer.

The defer statement will defer execution of the function until the outer function returns. A delayed function is evaluated immediately, but is not called until the outer function returns.

func main() {
	defer fmt.Println("world")

	fmt.Println("hello")}Copy the code

In this code, the original execution path is from the top down, printing “world” and then “Hello”. But because of the keyword defer, this line will be executed last, resulting in printing “hello” and then “world”.

Note that defer must be followed by a function call statement, not something else, or the compiler will report an error.

Scenarios to consider are file closure, or database connection release, etc., so that the open and closed code written together, not only makes the code more clean, but also prevents the developer after writing a long business code, forget to close the situation.

As to defer the underlying implementation, this article does not explain in detail, and simple it is to defer the statement at the back of the function calls into a stack, the address of the pressure in the current function is performed, the CPU will perform outside the function before the next line of code, the instruction address pop up in the stack to the CPU to perform first, until the stack is empty, didn’t end the function, Continue with the following code.

It can also be inferred from the above statement that if there are multiple refer statements, they will be executed from bottom to top.

Since this paper only compares various instructions, the detailed explanation of refer will be explained in the future.

6 a pointer

Pointers are familiar to any C or C++ developer; For Java developers, Pointers are a transparent thing to developers, an object takes up a certain amount of memory in the heap, and in the current frame, there’s a local variable whose value is the starting address of that object, which is also a pointer.

A pointer is a way for a developer to access memory, so to speak, but control is given to the developer or the virtual machine.

In Golang, Pointers are used in the same way as C. Again, ampersand gets the address, and star gets the value in the address.

However, unlike C, Golang has no pointer operation.

7 array

In Golang, arrays are defined like this:

var a [10]int
Copy the code

Doing so declares variable A as an array of 10 integers.

Note that in Golang, array sizes are also immutable, as in C.

Section 7.1

Slicing an array, as the name suggests, is the act of cutting out the desired parts of an array.

The size of each array is fixed. Slicing provides a dynamically sized, flexible view of array elements. In practice, slicing is more common than arrays.

Slices are defined by two subscripts, an upper bound and a lower bound, separated by a colon:

a[low : high]
Copy the code

It selects a half-open interval that includes the first element but excludes the last.

The following expression creates a slice containing elements in a with subscripts from 1 to 3:

a[1:4]
Copy the code

Here’s an example:

func main() {
	str := [4]string{
	    "aaa"."bbb"."ccc"."ddd",
	}
	fmt.Println(str)

	a := str[0:2]
	b := str[1:3]
	fmt.Println(a, b)

	b[0] = "XXX"
	fmt.Println(a, b)
	fmt.Println(str)
}
Copy the code

We define an array that contains “AAA “,” BBB “,” CCC “, and” DDD “. Then we define two slices, A and B. By definition, a is “AAA” and “BBB”, and B is “BBB” and “CCC”.

At this point, we change b[0] to “XXX”, so that B becomes “XXX” and “CCC”, which is unquestionable. But counterintuitively, the array STR at this point also becomes “AAA “,”XXX”,” CCC “,” DDD”.

This is because slicing in Golang, instead of copying, defines a new pointer to the memory space where the array was. So, if you change the value of the slice array, you also change the value of the original array.

In addition, slices can add elements with append. However, if the underlying array is not large enough, the slice will point to a reallocated array for copying.

Therefore, it can be concluded that:

  • Slicing does not store any data; it simply describes a segment of the underlying array.
  • Changing an element of a slice modifies the corresponding element in its underlying array.
  • Slices that share the underlying array with it observe these changes.

7.2 make

Slices can be created using the built-in function make, which is how you create dynamic arrays.

Before I do that, I need to explain two definitions, Len andcap(Capacity). Len is the length of the array, the length that the array was defined to be.capIs the capacity of the array, which refers to the length of the underlying array, or the length of the original array in memory. In the previous section, if I define a section of STR [0,0], the length will be 0, but the capacity will still be 5.Copy the code

The make function allocates an array with zero elements and returns a slice that references it:

a := make([]int, 5)  // len(a)=5
Copy the code

To specify its capacity, pass a third argument to make:

b := make([]int, 0, 5) // len(b)=0, cap(b)=5

b = b[:cap(b)] // len(b)=5, cap(b)=5
b = b[1:]      // len(b)=4, cap(b)=4
Copy the code

That is, the make function can customize the size of the slice. In Java parlance, it can be overloaded.

If there are only two arguments, the first argument is the type of the element in the array, and the second argument is the length of the array (where both length and capacity are 5).

If there is a third argument, the third argument can specify the size of the array, that is, how much memory the array can be allocated.

Write in the last

First of all, thank you for being here.

If this article can help you even a little bit, the author will be very happy!

Secondly, it should be noted that the author has just come into contact with Golang, and the purpose of writing this article is to play a note effect, so as to compare some grammatical differences between C, Java and Golang, and there must be a lot of cognitive errors. If you see any discrepancies in this article, please be sure to point out the author’s mistakes. If there is something the author does not understand, or you do not understand, you are also welcome to leave a message, exchange learning progress together.

In addition, there are many areas that have not been deeply explored in this article, which are documented by these authors and will be analyzed from a source perspective in future articles. In this article, just learn how to use it, and achieve the goal.

So in the end, thank you again

PS: If you have other questions, you can also find the author on the official account. And, all articles will be updated in the public account at the first time, welcome to find the author to play ~