Author: ReganYue

Source: Hang Seng LIGHT Cloud Community

Go language learning

A note on the naming of function return parameters

What’s wrong with this function?

func fun(x, y int) (s int, error) {
  return x * y, nil
}
Copy the code

While this error is flagged in integrated development environment Goland, other development tools, such as VS Code, are not known to be flagged.

We can see the hint that the function has named return parameters and no named return parameters.

This means that when a function has multiple return parameters, if you name one parameter, the other parameters must also be named. And if you name an argument, you must bracket it, regardless of whether it has one or more arguments. The first parameter is named s, but the second parameter is not named, so there is an error.

What is the difference between new() and make()?

In the Go SDK, new is described like this:

// The new built-in function allocates memory. The first argument is a type,
// not a value, and the value returned is a pointer to a newly
// allocated zero value of that type.
func new(Type) *Type
Copy the code

It is a built-in function that allocates memory and takes only one argument. If you use this function, it returns a pointer to a chunk of newly cleared memory that has a zero value for the type represented by the first argument.

Let’s look at make again:

// The make built-in function allocates and initializes an object of type // slice, map, or chan (only). Like new, the first argument is a type, not a // value. Unlike new, make's return type is the same as the type of its // argument, not a pointer to it. The specification of the result depends on // the type: // Slice: The size specifies the length. The capacity of the slice is // equal to its length. A second integer argument may be provided to // specify a different capacity; it must be no smaller than the // length. For example, make([]int, 0, 10) allocates an underlying array // of size 10 and returns a slice of length 0 and capacity 10 that is // backed by this underlying array. // Map: An empty map is allocated with enough space to hold the // specified number of elements. The size may be omitted, in which case // a small starting size is allocated. // Channel: The channel's buffer is initialized with the specified // buffer capacity. If zero, or the size is omitted, the channel is // unbuffered. func make(t Type, size ... IntegerType) TypeCopy the code

Make allocates memory, but only for Slice, Map, or chan. And instead of returning a pointer, make returns the value of the type represented by your first argument.

With that said, let’s take a look to see if this code compiles.

​
import "fmt"
​
func main() {
  l := new([]int)
  l = append(l, 0)
  fmt.Println(l)
}
​
Copy the code

Obviously not, here is an error message:

As we saw earlier, the new function new produces a pointer, and Pointers don’t append. It is better to use make instead of new to create slice, map, or chan.

3. Section addition section problem

** If you have two slices, how do you use Append to put them together in one slice? **

Is that ok?

package main
​
import "fmt"
​
func main() {
  slice := []int{8, 8, 8}
  append_slice := []int{2, 8}
  slice = append(slice, append_slice)
  fmt.Println(slice)
}
​
Copy the code

Take a look at what Goland suggests.

I don’t think so.

This is where we use… This grammar sugar.

It has several functions, one of which is the ability to break up slices for transmission. And when you define a function, you can take any parameter.

Here is the result:

4. Limitations on short schema declaration variables

Let’s take a look at the following code. Do you see any problems?

package main
​
import "fmt"
​
​
var(
  two = 200
)
one := 100
​
func main() {
  fmt.Println(one,two)
}
Copy the code

There are problems.

Let’s talk about the limitations of short schema declarations for variables:

  1. You must use display initialization, which is to give initial values manually.
  2. You cannot specify a data type; the compiler automatically inferences the type of the variable based on the initial value you specify.
  3. You can only declare variables in short mode inside functions.

The reason for our error here is that the third constraint above is triggered: the variable is not declared using short mode inside the function.