SoFunction
Updated on 2025-03-03

Golang compiled 49 interview questions summary (multiple choice)

Golang's 100 questions compiled

Ability model (test)

Primary primary:

Familiar with basic syntax and able to understand the intention of the code;
Under the guidance of others, the code written complies with the CleanCode specification;

Intermediate:

Ability to independently complete the development and testing of user stories;
Ability to smell the bad smell of code and know how to refactor it to achieve your goal;

Senior:

Ability to develop high-quality and high-performance code;
Ability to proficient in using advanced features, develop programming or testing frameworks;

Multiple choice questions

1. [Junior] The following keywords are ()

A. func

B. def

C. struct

D. class

Reference answer: AC

2. [Elementary] Define a global string variable in the package. The following syntax is correct ()

A. var str string

B. str := ""

C. str = ""

D. var str = ""

Reference answer: AD

3.   【Junior】Access its member variable name through pointer variable p. The following syntax is correct ()

A.

B. (*p).name

C. (&p).name

D. p->name

Reference answer: AB

4. [Junior] The statement about interfaces and classes, the correct statement below is ()

A. A class only needs to implement all the functions required by the interface. Let’s say that this class implements the interface.

B. When implementing a class, you only need to care about what methods you should provide, and no longer have to worry about how detailed the interface needs to be disassembled to make sense.

C. When implementing an interface, you need to import the package where the interface is located.

D. The interface is defined by the user according to its own needs. The user does not need to care whether other modules have defined similar interfaces.
Reference answer: ABD

5. [Elementary] Regarding string concatenation, the following syntax is correct ()

A. str := ‘abc’ + ‘123’

B. str := "abc" + "123"

C. str := '123' + "abc"

D. ("abc%d", 123)

Reference answer: BD

6. [Junior] Regarding coroutines, the following statement is correct ()

A. Both coroutines and threads can implement concurrent execution of programs.

B. Threads are lighter than coroutines

C. There is no deadlock problem for coroutines

D. Communication between coroutines through channel

Reference answer: AD

7.   【Intermediate】 Regarding the init function, the correct statement below is ()

A. A package can contain multiple init functions

B. When compiling the program, first execute the init function of the import package, and then execute the init function in this package.

C. There cannot be an init function in the main package

D. The init function can be called by other functions

Reference answer: AB

8. [Elementary] Regarding loop statements, the following statements are correct ()

A. Loop statements support both for keywords, while and do-while

B. The basic usage method of keyword for is not different from that in C/C++

C. for loops support continue and break to control loops, but it provides a more advanced break, which can choose which loop to interrupt

D. The for loop does not support multiple assignment statements separated by commas, and multiple variables must be initialized using parallel assignment.

Reference answer: CD

9.【Intermediate】For function definition:

func add(args...int)int{
    sum:=0
    for_,arg:=range args{
        sum += arg
        }
return sum
}

The correct call to the add function below is ()

A. add(1, 2)

B. add(1, 3, 7)

C. add([]int{1, 2})

D. add([]int{1, 3, 7}...)

Reference answer: ABD

10. [Elementary] Regarding type conversion, the following syntax is correct ()

A.

type MyInt int
var i int = 1
var jMyInt = i

B.

type MyIntint
var i int= 1
var jMyInt = (MyInt)i

C.

type MyIntint
var i int= 1
var jMyInt = MyInt(i)

D.

type MyIntint
var i int= 1
var jMyInt = i.(MyInt)

Reference answer: C

11. [Elementary] Regarding the initialization of local variables, the correct way to use them is ()

A. var i int = 10

B. var i = 10

C. i := 10

D. i = 10

Reference answer: ABC

12. [Elementary] Regarding the definition of const constants, the correct way to use them is ()

A.

const Pi float64 = 3.14159265358979323846
const zero= 0.0

B.

const (
size int64= 1024
eof = -1
)

C.

const (
ERR_ELEM_EXISTerror = ("element already exists")
ERR_ELEM_NT_EXISTerror = ("element not exists")
)

D.

const u, vfloat32 = 0, 3
const a,b, c = 3, 4, "foo"

Reference answer: ABD

13. [Elementary] Regarding the assignment of the Boolean variable b, the following wrong usage is ()

A. b = true

B. b = 1

C. b = bool(1)

D. b = (1 == 2)

Reference answer: BC

14. [Intermediate] The running result of the following program is ()

func main() {  
    if (true) {
        defer ("1")
    } 
    else {
        defer ("2")
    }
    ("3")
}

A. 321

B. 32

C. 31

D. 13

Reference answer: C

14. [Elementary] Regarding switch statements, the following statements are correct ()

A. The conditional expression must be a constant or an integer

B. In a single case, multiple result options can appear

C. You need to use break to explicitly exit a case

D. Only by explicitly adding the fallthrough keyword in the case will the next case that follows will continue to be executed.

Reference answer: BD

15. [Intermediate] There is no hidden this pointer in golang. The meaning of this sentence is ()

A. The object imposed by the method is explicitly passed and is not hidden

B. golang follows many concepts in traditional object-oriented programming, such as inheritance, virtual functions and constructors.

C. Golang's object-oriented expression is more intuitive, and it only changes to process-oriented expression in a grammatical form.

D. The object applied by the method does not need to be a pointer, nor does it need to be called this

Reference answer: ACD

16. [Intermediate] The reference types in golang include ()

A. Array slice

B. map

C. channel

D. interface

Reference answer: ABCD

17. [Intermediate] The pointer operation in golang includes ()

A. You can perform self-increment or self-decrease operation on the pointer

B. The address of the pointer can be retrieved by "&"

C. The data pointed to by the pointer can be fetched by "*"

D. You can perform subscript operation on the pointer

Reference answer: BC

18. [Elementary] Regarding the main function (the starting point of the execution of the executable program), the correct statement below is ()

A. The main function cannot take parameters

B. The main function cannot define the return value

C. The package where the main function is located must be the main package

D. The flag package can be used in the main function to obtain and parse command line parameters.

Reference answer: ABCD

19. [Intermediate] The correct assignment below is ()

A. var x = nil

B. var x interface{} = nil

C. var x string = nil

D. var x error = nil

Reference answer: BD

20. [Intermediate] Regarding the initialization of integer slices, the correct one below is ()

A. s := make([]int)

B. s := make([]int, 0)

C. s := make([]int, 5, 10)

D. s := []int{1, 2, 3, 4, 5}

Reference answer: BCD

21. [Intermediate] Delete an element from the slice, the correct implementation of the following algorithm is ()

A.

func (s *Slice)Remove(value interface{})error {
    for i, v := range *s {
        if isEqual(value, v) {
            if i== len(*s) - 1 {
                *s = (*s)[:i]
            }
        else {
                *s = append((*s)[:i],(*s)[i + 2:]...)

        }

         return nil
        }
    }
return ERR_ELEM_NT_EXIST
}

B.

func (s*Slice)Remove(value interface{}) error {
for i, v:= range *s {
    if isEqual(value, v) {
        *s =append((*s)[:i],(*s)[i + 1:])
         return nil
    }
}
returnERR_ELEM_NT_EXIST
}

C.

func (s*Slice)Remove(value interface{}) error {
for i, v:= range *s {
    if isEqual(value, v) {
        delete(*s, v)
        return nil
    }
}
returnERR_ELEM_NT_EXIST
}

D.

func (s*Slice)Remove(value interface{}) error {
for i, v:= range *s {
    if isEqual(value, v) {
        *s =append((*s)[:i],(*s)[i + 1:]...)
        return nil
    }
}
returnERR_ELEM_NT_EXIST
}

Reference answer: D

22. [Junior] For the assignment of local variable integer slice x, the correct definition below is ()

A.

 x := []int{
 1, 2, 3,
 4, 5, 6,
}

B.

x :=[]int{
1, 2, 3,
4, 5, 6
}

C.

x :=[]int{
1, 2, 3,
4, 5, 6}

D.

x :=[]int{1, 2, 3, 4, 5, 6,}

Reference answer: ACD

23. [Elementary] Regarding the autoincrement and self-decrease operation of variables, the correct statement below is ()

A.

i := 1
i++

B.

i := 1
j = i++

C.

i := 1
++i

D.

i := 1
i--

Reference answer: AD

24. [Intermediate] Regarding function declaration, the following syntax error is ()

A. func f(a, b int) (value int, err error)

B. func f(a int, b int) (value int, err error)

C. func f(a, b int) (value int, error)

D. func f(a int, b int) (int, int, error)

Reference answer: C

25. [Intermediate] If the calling code of the Add function is:

 func main() {
 var a Integer = 1
 var b Integer = 2
 var i interface{} = &a
 sum := i.(*Integer).Add(b)
 (sum)
}

Then the correct definition of Add function is ()

A.

typeInteger int
func (aInteger) Add(b Integer) Integer {
 return a + b
}

B.

typeInteger int
func (aInteger) Add(b *Integer) Integer {
 return a + *b
}

C.

typeInteger int
func (a*Integer) Add(b Integer) Integer {
 return *a + b
}

D.

typeInteger int
func (a*Integer) Add(b *Integer) Integer {
 return *a + *b
}

Reference answer: AC

26. [Intermediate] If the calling code of the Add function is:

func main() {
var a Integer = 1
 var b Integer = 2
 var i interface{} = a
sum := i.(Integer).Add(b)
 (sum)
}

Then the correct definition of Add function is ()

A.

typeInteger int
func (a Integer)Add(b Integer) Integer {
 return a + b
}

B.

typeInteger int
func (aInteger) Add(b *Integer) Integer {
 return a + *b
}

C.

typelnteger int
func (a*Integer) Add(b Integer) Integer {
 return *a + b
}

D.

typeInteger int
func (a*Integer) Add(b *Integer) Integer {
 return *a + *b
}

Reference answer: A

27. [Intermediate] Regarding the definition of GetPodAction, the correct assignment below is ()

 type Fragment interface {
 Exec(transInfo *TransInfo) error
 }
 type GetPodAction struct {
 }
 func (g GetPodAction) Exec(transInfo*TransInfo) error {
 ...
 return nil
}

A. var fragment Fragment =new(GetPodAction)

B. var fragment Fragment = GetPodAction

C. var fragment Fragment = &GetPodAction{}

D. var fragment Fragment = GetPodAction{}

Reference answer: ACD

28. [Intermediate] Regarding GoMock, the correct statement below is ()

A. GoMock can drive piles on the interface

B. GoMock can pile member functions of class

C. GoMock can drive piles on functions

D. Dependency injection after GoMock pile driving can be completed through GoStub

Reference answer: AD

29. [Intermediate] Regarding the interface, the correct statement below is ()

A. As long as the two interfaces have the same method list (it doesn't matter if the order is different), then they are equivalent and can be assigned to each other.

B. If the method list of interface A is a subset of the method list of interface B, then interface B can be assigned to interface A

C. Whether the interface query is successful can only be determined during the runtime.

D. Whether the interface assignment is feasible can only be determined during the runtime.

Reference answer: ABC

30. [Elementary] Regarding channel, the following syntax is correct ()

A. var ch chan int

B. ch := make(chan int)

C. <- ch

D. ch <-

Reference answer: ABC

31. [Elementary] Regarding synchronous lock, the correct statement below is ()

A. When a goroutine obtains Mutex, other goroutines can only wait obediently unless the goroutine releases the Mutex

B. RWMutex will prevent writing when the read lock is occupied, but will not prevent reading.

C. RWMutex will prevent any other goroutine (regardless of read and write) from entering when the write lock is occupied. The entire lock is equivalent to being exclusive to the goroutine.

D. The Lock() operation needs to be guaranteed that the Unlock() or RUnlock() call corresponds to it.

Reference answer: ABC

32. [Intermediate] Most data types in golang can be converted into valid JSON text, except for the following types ()

A. Pointer

B. channel

C. complex

D. Function

Reference answer: BCD

33. [Intermediate] Regarding go vendor, the correct statement below is ()

A. The basic idea is to place the source code of the referenced external package in the vendor directory of the current project.

B. When compiling go code, you will give priority to looking for dependency packages from the vendor directory first.

C. You can specify external packages that refer to a specific version

D. With the vendor directory, you can compile it by packaging the current project code to $GOPATH/src of other machines.

Reference answer: ABD

34. [Junior] flag is a bool-type variable. The following if expression conforms to the encoding specification is ()

A. if flag == 1

B. if flag

C. if flag == false

D. if !flag

Reference answer: BD

35. [Junior] value is an integer variable, and the following if expression conforms to the encoding specification is ()

A. if value == 0

B. if value

C. if value != 0

D. if !value

Reference answer: AC

36. [Intermediate] Regarding the wrong design of the function return value, the following statement is correct ()

A. If there is only one reason for failure, return bool

B. If there is more than one reason for failure, return error

C. If there is no cause of failure, no bool or error is returned.

D. If retrying several times can avoid failure, do not return bool or error immediately

Reference answer: ABCD

37. [Intermediate] Regarding exception design, the correct statement below is ()

A. In the program development stage, insist on quick errors and cause the program to crash abnormally

B. After the program is deployed, exceptions should be restored to avoid program termination.

C. Everything is wrong, no exception design is required

D. Use exception handling for branches that should not appear
Reference answer: ABD

38. [Intermediate] Regarding slice or map operation, the correct one below is ()

A.

var s []int
s =append(s,1)

B.

var mmap[string]int
m["one"]= 1

C.

var s[]int
s =make([]int, 0)
s =append(s,1)

D.

var mmap[string]int
m =make(map[string]int)
m["one"]= 1

Reference answer: ACD

39. [Intermediate] Regarding the characteristics of channel, the correct statement below is ()

A. Send data to a nil channel, causing permanent blockage

B. Receive data from a nil channel, causing permanent blockage

C. Send data to a closed channel, causing panic

D. Receive data from a closed channel, and if the buffer is empty, a zero value is returned.
Reference answer: ABCD

40. [Intermediate] Regarding unbuffered and conflicting channels, the correct statement below is ()

A. Unbuffered channel is the default channel with buffering of 1.

B. Both unbuffered channel and buffered channel are synchronized.

C. Both unbuffered channels and buffered channels are asynchronous.

D. Unbuffered channel is synchronous, while buffered channel is asynchronous.

Reference answer: D

41. [Intermediate] Regarding the triggering of exceptions, the following statement is correct ()

A. Null pointer analysis

B. Subscript crosses the boundary

C. Divider is 0

D. Call panic function

Reference answer: ABCD

42. [Intermediate] Regarding the applicable types of cap functions, the following statement is correct ()

A. array

B. slice

C. map

D. channel

Reference answer: ABD

43. [Intermediate] Regarding beego framework, the correct statement below is ()

A. beego is a lightweight HTTP framework implemented by golang

B. Beego can complete URL routing injection through various methods such as annotation routing, regular routing, etc.

C. You can use the bee new tool to generate empty projects, and then use the bee run command to automatically hot compile.

D. The beego framework only provides processing of url routing, while the database part in the MVC architecture does not provide framework support.

Reference answer: ABC

44. [Intermediate] Regarding goconvey, the correct statement below is ()

A. goconvey is a unit testing framework that supports golang

B. goconvey can automatically monitor file modification and start tests, and can output test results to the web interface in real time.

C. goconvey provides rich assertions to simplify the writing of test cases.

D. goconvey cannot integrate with go test

Reference answer: ABC

45. [Intermediate] Regarding go vet, the correct statement below is ()

A. go vet is the packaging of go tool vet, which comes with golang.

B. When executing go vet database, all subfolders in the directory where the database is located can be recursively detected.

C. go vet can specify the packet to be detected using an absolute path, a relative path, or a path relative to GOPATH.

D. go vet can detect dead code

Reference answer: ACD

46. ​​[Intermediate] Regarding map, the correct statement below is ()

A. The entry parameter during map deserialization must be the address of the map

B. Passing map in function call, then the increase of map elements in child function will not lead to modification of map in parent function

C. Passing map in function call, then the modification of map elements in child function will not lead to the modification of map in parent function.

D. Cannot use the built-in function delete to delete elements of map

Reference answer: A

47.    [Intermediate] Regarding GoStub, the correct statement below is ()

A. GoStub can drive piles on global variables

B. GoStub can drive piles on functions

C. GoStub can drive piles on class member methods

D. GoStub can drive dynamic piles. For example, after staking a function, calling the function multiple times will have different behaviors.

Reference answer: ABD

48.   【Junior level】 Regarding the select mechanism, the correct statement below is ()

A. Select mechanism is used to deal with asynchronous IO problems

B. The biggest limitation of the select mechanism is that each case statement must have an IO operation

C. golang supports select keywords at the language level

D. The usage of the select keyword is very similar to that of the switch statement, and the judgment conditions are required later.

Reference answer: ABC

49. [Elementary] Regarding memory leakage, the correct statement below is ()

A. Golang has automatic garbage collection, no memory leaks

B. The main purpose of detecting memory leaks in golang is the pprof package.

C. Memory leaks can be discovered during the compilation stage

D. You should regularly use a browser to view the system's real-time memory information and promptly discover memory leaks.

Reference answer: BD

The above is the detailed content of the 49 interview questions compiled by Golang (multiple-choice questions). For more information about Go interview questions, please pay attention to my other related articles!