Learn Go

Tram Ho

Introduce

Hello everyone, let’s continue learning Go on day 2. Let’s go!!!

Content

Pointers

Pointer is a variable that holds the address of an area in memory. Instead of storing the value directly into the variable, we can store the address of that value and use the pointer to access that value. Using pointers in Go can help improve performance and save memory compared to copying data between variables.

To create a pointer, you use the “&” operator to get the address of a variable, and the “*” operator to access the value at that address.

syntax:

  • “&” operator:
  • Operator “*” :

For example:

Structs

Struct is a composite data type that allows you to define a collection of fields of different data types. It helps you organize and store related data together in the same object.

We can create a Vertex object by assigning values ​​to the data fields of the struct.

We can access and assign values ​​to the data fields of a struct using the “.” .

We can access and modify the contents of the struct using Pointer.

Arrays

An array is declared by specifying the type of data to be stored and the length of the array.

Syntax:

e.g. Create array a of length 10 with elements of type int.

Note: Once declared, the length and data type of the array cannot be changed. If you want to change the length, you have to create a new array and copy the data from the old array.

Zero value

When declaring an array without assigning a value, all elements will be assigned a default value according to the data type (int = 0, string = “”).

For example, array [5]int will have all elements with value 0.

init-array.png

Literal

Calculating Size

If the length is given as “…” , Go will determine the length of the array based on the number of initialized elements.

Specific elements

If you know the length of the array you need, but only initialize specific elements, you can use this syntax.

Pointer elements

You can have an array of pointers. You use the * operator to access the value of each pointer the element points to.

The values ​​of the array declared in the array look like the image below: Screenshot 2023-02-11 at 18.13.11.png

An array is a value in Go. This means you can use it in an assignment operation. Thus, an array can be assigned to other arrays of the same type and length.

As for Pointer, when we assign, we assign the memory cell addresses of the pointer, not the values ​​that the pointer is pointing to.

It looks like the image below: Screenshot 2023-02-11 at 18.29.00.png

Multidimensional array (multidimensional array)

Multidimensional is a data structure that allows to store elements as a multidimensional table. Each element in a multidimensional array is identified by multiple indices called indexes.

Like the picture below: Screenshot 2023-02-11 at 18.46.12.png

Passing arrays between functions

When passing an array between functions, it can take a lot of memory and affect performance. Arrays are passed by copying the entire array to the function regardless of its size.

When calling handleNum with an array of 8 megabytes , the entire array will be copied and allocated on the stack, which consumes a lot of memory and affects performance. The better way is to pass a pointer to the array and let just copy 8 bytes .

summary

So after 1 day I learned:

  • Pointers
  • Structs
  • Arrays

If you have any questions or need further clarification, you can comment below. Thank you for following my post. Wish you have a nice day. Tks

Share the news now

Source : Viblo