Go language learning checking and patching Day2

Author: Regan Yue

Source: Hang Seng LIGHT cloud community

Go language learning checking and patching Day2

1, A note on the naming of function return parameters

Please observe the following function. Is there any problem?

func fun(x, y int) (s int, error) {
    return x * y, nil

Although this error will be prompted in the integrated development environment Golan, other development tools, such as VS Code, do not know whether it will be prompted.

We can see this prompt: the function has named return parameters and no named return parameters.

This means that when a function has multiple return value parameters, if you name one parameter, other parameters must also be named. Moreover, if you name a parameter, you must add parentheses to the parameter, whether the number of parameters is one or more. Here, the first parameter is named s, but the second parameter is not named, so there is an error.

2, What's the difference between new() and make()?

In the Go SDK, new is described as follows:

// 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

It is a built-in function for allocating memory with only one parameter. If this function is used, it will return a pointer to a newly opened memory, which is the zero value of the type represented by the first parameter.

Let's take another look at make:

// 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) Type

We can see that make also allocates memory, but it can only allocate memory to slice, map or chan. And this make does not return a pointer, but returns the value of the type represented by your first parameter.

After the above introduction, let's see if this code can be compiled.

import "fmt"

func main() {
    l := new([]int)
    l = append(l, 0)

Obviously not. The following is the error message:

As we mentioned earlier, the new function is a pointer, and the pointer cannot perform the append operation. Therefore, when we build slice, map or chan, we'd better use the make function instead of the new function.

3, Slice additional slice problem

If there are two slices, how to use append to piece them together in one slice?

Is that all right?

package main

import "fmt"

func main() {
    slice := []int{8, 8, 8}
    append_slice := []int{2, 8}
    slice = append(slice, append_slice)

Let's see what Golan said.

I don't think so.

Then we have to use... This grammar sugar.

It has several functions, one of which is to break up the slices for transmission. It is also used when defining a function, which can receive any parameter.

Here are the results:

4, Restrictions on short mode declaration variables

Let's take a look at the following code. Do you think there is any problem?

package main

import "fmt"

    two = 200
one := 100

func main() {

There is a problem.

Let's talk about the limitations of the short schema declaration of variables:

  1. Display initialization must be used, that is, the initial value must be given manually.
  2. The data type cannot be specified. The compiler will automatically infer the type of variable according to the initial value you specify.
  3. Variables can only be declared using short patterns inside functions.

The reason for our error here is that the third limitation above is triggered: no short pattern is used inside the function to declare variables.

Tags: Go Programming

Posted on Fri, 03 Dec 2021 15:12:40 -0500 by journy101