preface

I just graduated from node development last year, and now I have joined the team of learning go. So, since I am a node development, I will start with the most basic, and compare the data types in Node (namely javascript) with the data types in GO. What are the similarities and differences?

There are two main data types

In the process of programming, whether Java or javascript, you will be exposed to the concept that the data type can be generally divided into two types, one is the value type, and the other is the reference type. In other words, when passing, can be divided into value passing and reference passing.

The main difference between the two data types is that during the reference process, the value type copies the current variable to each other, while the reference type refers to the data structure by pointing to the location, that is, the two variables refer to data from the same place.

So what don’t you want to do when you’re using these two data structures.

Value types and reference types in javascript

Value types

In javascript, all simple data types are value types, such as number, string, bool, and so on. When these data types are referenced, they change the existing data without changing the original data (a bit of a wrap here).

Here’s a simple example

var a = 12
var b = "hello"
var c = true

function text(a,b,c) {
    var a1 = a
    var b1 = b
    var c1 = c

    a1 = 13  
    b1 = "world"
    c1 = false

    console.log(a, a1) / / 12 and 13
    console.log(b, b1) // hello, world
    console.log(c, c1) // true, false
}
text(a,b,c)
Copy the code

Here we can see that the initial a, B, and C are not changed by subsequent changes in assignment.

Reference types

In javascript, all complex data structures, when passed, are reference types, such as arrays, objects, etc.

A simple example of a complex data type that has an impact on the original reference object when it is assigned a second time to a reference variable is used

 var a = [1.2.3.4]
 
 function test(a) {
     var b = a 

     b[1] =10
     console.log(a) / /,10,3,4 [1]
     console.log(b) / /,10,3,4 [1]
 }
 test(a)
Copy the code

So what we can see here is that when we change B, we change A.

Are there two types of data transfer in GO?

Here’s a straightforward conclusion: In GO, all data passes are value passes, that is, a copy route, and that includes the array mentioned above

Note: Because GO is a strongly typed language and javascript is a weakly typed language, there are some differences when declaring a variable.

Here’s a small example using GO

func main(a) {
	a := [3]int {1.2.3} Var a = [3]int {1,2,3}
	b := a
	b[1] = 10
	fmt.Print(a,b) // This step is equivalent to console.log()
        // [1 2 3] [1 10 3]
}
Copy the code

As we can see from this very simple example, in the case of an array, it is also a type of value passing.

So isn’t there really a way to do reference passing, which would be very cumbersome when writing code? Of course there is.

Pointer and Slice

For many front-end or javascript developers like me, the concept of Pointers may be a little unfamiliar, because in javascript there is no such thing as Pointers (only references), whereas in GO, it can be done by means of Pointers.

// This is an example of switching two numbers
func sort(a,b int)  {
	a , b = b , a
}

func main(a) {
	var a, b int = 2.3
	sort(a,b)
	fmt.Println(a,b) / / 2, 3
}
Copy the code

And here we can see that it’s still the same value, because in GO, all of the passing types are value passing.

So, when we use Pointers, we can solve this problem

func sort(a,b *int)  {
	*a , *b = *b , *a
}

func main(a) {
	var a, b int = 2.3
	sort(&a,&b)
	fmt.Println(a,b) / / 3 and 2
}
Copy the code

One more thing to note here: In GO, Pointers cannot be evaluated (as if C were).

In the same way, arrays can be passed by reference through Pointers.

func chunk(a *[3]int)  {
	a[2] = 10
}

func main(a) {
	var a = [3]int {1.2.3}
	chunk(&a)
	fmt.Println(a) / / 10 [1, 2]
}
Copy the code

Although the effect is ok, but in the process of use, but very trouble, the first in a strongly typed language like Go inside, the length of the array is the need to booked in advance, but not like javascript, feel free to write, pointer reference at the same time also very trouble, so is there a way, you can achieve in using Pointers in the array, The same effect?

There is a special type of slice in Go compared to other languages

Some concepts of slicing

A Go slice is an abstraction of an array.

Go provides a flexible and powerful built-in type of slice (” dynamic array “). Slices are not fixed in length compared to arrays, and elements can be appented, which may increase the size of the slice.

So slices are going to be used differently

// Declare a slice

func main(a) {
	var a = [...]int {1.2.3}
	b := [...]int {4.5.6}
	fmt.Println(a, b) / / [1, 2, 3], [4 and 6]
}
Copy the code

Here we can see that when declaring a slice, you don’t need to specify the length of an array, but you can configure it flexibly.

For example, I want to concatenate two slices, or I want to append data to one slice.

func concat(a, b []int)  {
	c := append(a, b...) // This is similar to deconstruction in ES6, which breaks up slices
	fmt.Print(c) // [1 2 3 4 5 6]
}

func push(a []int, b int)  {
	d := append(a,b)
	fmt.Print(d) // [1 2 3 9]
}

func main(a) {
	a := []int {1.2.3}
	b := []int {4.5.6}
	concat(a,b)
	push(a,9)}Copy the code

Some of the concepts of slicing, and how to use it in detail, will be explained in a future article, but let’s go back to the question of how to pass types, like arrays, by reference.

What does it look like when we’re using slices, when we’re passing values in a function? Let’s look at the following example.

func update(a []int)  {
   a[2] = 10
}

func main(a) {
   // Splicing of slices
   a := []int{1.2.3}
   update(a)
   fmt.Print(a) / / 10 [1, 2]
}
Copy the code

As you can see, by slicing an array structure (although it is not an array), you can successfully pass it by reference and avoid a lot of trouble.

The last

In the future, as I continue to further study the GO language, I will add more articles related to Go and compare it with the familiar Node, hoping to deepen my understanding of this language.

If feel my article has where wrong place, welcome comment to correct, if feel the article content also pass go, that comes a key triple couplet!!