This is the third day of my participation in the August Text Challenge.More challenges in August

Go one thousand and one questions is to summarize and comb and analyze the principles of GO learning and interview related questions, which is an accumulating series. One thousand and one questions is not really one thousand and one questions but an estimate.

1. Difference between make and new

  • make onlycanInitialize thegobuilt-intheThe data structureSlice, map, chan, and return the type itselfNot a pointer.
  • newAllocates memory based on the type passed in and initializes zero, andreturnThat points to this memory spacePointer to the.

Common: Both can allocate memory.

Difference: Make only works with the built-in Slice, map, and chan types of Go and returns the type passed in. New can operate on any type and the return type is a pointer (pointer to the allocated memory space) that initializes the zero value of the corresponding type.

Why doesn’t make return a pointer?

Since the go built-in types slice, map, and chan are themselves reference types, the built-in structure actually has Pointers, so there is no need to return Pointers.

2. When a function call needs to pass in a structure, pass a pointer or a value? How do you tell when to use which?

First of all, there are no reference types in GO, and all function passes are value passes, but such types as Slice, map, and channel have Pointers in their built-in structures, so passing these types will modify the original data.

  • Value transfer: do not want to change the original data, just need the data for use.
  • Pointer passing: It is more efficient to pass Pointers if you want to change the original data or if you want to be efficient.

3. What is the difference between nil slice and empty slice?

Since Pointers exist in the slice built-in structure, the difference is whether or not Pointers are made space.

  • Nil slice == nil is completely empty and uninitialized, and the built-in pointer array has no space
  • Empty slice can be understood as empty data, memory space has been allocated, and the built-in pointer array has allocated space to point to memory address. Empty slice == nil is not valid, so len(Empty slice) == 0 is required to judge empty data
infrastructure nil slice empty slice
array 0 0x6736adb0
Len 0 0
Cap 0 0
Compared with nil True False
// Slice built-in structure
type slice struct {
    array unsafe.Pointer
    len   int
    cap   int
}
Copy the code

4. What’s the difference between arrays and slice?

See the “Slice” and “array []” differences and common error analysis in go for details

  • Array: An array is a fixed length sequence of elements of a particular type. An array can consist of zero or more elements.

  • Slice: Slice represents a variable-length sequence in which each element has the same type. A slice type is written as []T, where T stands for the type of the slice element; Slice’s syntax is similar to that of an array, except that there is no fixed length.

  • Difference between contrast

Biggest difference: Variable slice length

Difference between contrast An array of slice
The length of the fixed variable
The element type Single and fixed Single and fixed
Constituent parts A series of elements Pointers, Length (len), and capacity (Cap) (underlying reference to an array object)
Pointer to Points to the address of the first element of the array The address that points to the underlying array element corresponding to the first slice element. Note that the first slice element is not necessarily the first array element
Initialize the The default value is zero, and the initial length is required The default value is zero, and no initialization length is required

5. What is the difference between a nil map and an empty map?

Nil maps and empty maps are basically the same, but operationally different

  • They can all read values, but they’re all null
  • Null maps can be assigned, nil maps cannot
  • It’s empty, but it can be useddeleteDelete operation (earlier Go did not support delete of nil map)

6. How to determine whether map has (exists) values?

// Create an empty map
m := map[string]int{}
value, ok := m["1"]
// Ok is false because it does not exist. Value The default value of the corresponding type is 0
Copy the code

7. How do I stop a running Goroutine?

You can pass a channel, you can pass a context, you can pass a different channel if you want.

go func(a) {
    for {
        select {
        case <-ctx.Done:
          return
        case <-quit:
            return
        default:
            / /...}}} ()Copy the code

8. How to tell if a channel is closed?

ch := make(chan int)
close(ch)
value, isClose := <-chan
// isClose will get the state of whether the channel is closed or not, which is already closed so it gets false
// The value can be obtained from the closed channel, which is the default zero value for the corresponding type
Copy the code

9. Some considerations when using channel

  • Close (channel)
  • Cannot write data to a closed channel, panic will occur
  • It is possible to read data from a closed channel, but to read a zero value of the corresponding type

10. Have you used select before? What is it used for?

Select from go is listening for an IO operation. When an IO operation occurs, the corresponding action is triggered. Each case statement must contain an IO operation, specifically, a channel-oriented IO operation.

select {
case <-ch:
  // ...
}
Copy the code

At the end

Go 1001 Questions is an ongoing series designed to provide go learning related, GO interviews, GO principle analysis, and go related libraries. We hope that you can support and share 👍🏻 and go 1001 can be more rich!

In this issue, 10 interview questions of go basic part were explained first, and the second phase continued to explain the interview questions of Go basic part.