Top 50 Go (Golang) Interview Question & Answers

Golang Interview Questions for Freshers
  • What is Golang? …
  • Why should one learn Golang? …
  • What are Golang packages? …
  • Is Golang case sensitive or insensitive? …
  • What are Golang pointers? …
  • What do you understand by Golang string literals? …
  • What is the syntax used for the for loop in Golang?

Top 30 Go Interview Questions And Answers | Best Golang Interview Questions [Updated 2022]

Question 7: Can you change a specific character in a string?

No. Strings are immutable (read-only) data types and you cannot change them. If we try to change a specific character in a string, we’ll get a runtime error.

Q15. How to return multiple values from a function?

We can return multiple values from a function in Golang, the below code shows how we can return multiple values

Golang’s concurrency model and small syntax make Golang fast programming language, Golang compilation is very fast, Go hyperlinks all the dependency libraries into a single binary file, as a result, putting off the dependence on servers.

Question 11: Explain the backing array of a slice value.

When we create a slice, Go creates a hidden array behind the scenes, called backing or underlying array, and the new slice type variable refers to it. The backing array stores the elements, not the slice.

Go implements a slice as a data structure called slice header, which is the runtime representation of the slice.

It contains three fields:

  • The address of the backing array (a pointer to the first element of the backing array).
  • The length of the slice. The built-in function len() returns it.
  • The capacity of the slice which is the size of the backing array after the first element of the slice. It’s returned by the cap() built-in function.
  • Note that a nil slice doesn’t have a backing array, so all the fields in the slice header are equal to zero.

    Q42. How to check the variable type at runtime in Golang?

    In Golang, to check the variable type at runtime, a special type of switch is used and is referred to as a type switch. Also, you can switch on the type of interface value with Type Switch.

    Question 10: Give an example of an array and slice declaration.

    Here’s an example of declaring and initializing an array of type [4] string using the short declaration syntax.

    friends := [4]string{“Dan”, “Diana”, “Paul”, “John”}

    Here’s an example of declaring and initializing a slice of type [] int using the short declaration syntax.

    numbers := []int{2, 3, 4, 5}

    Question 27: How could you detect a data race in Go code?

    Starting with Go 1.1, a new tool called race detector for finding race conditions in Go code was made available.

    Using the race detector is simple. We just add a -race flag to our normal Go command-line tool.

    When the -race command-line flag is set, the compiler inspects all memory accesses with code that records when and how the memory was accessed. In the meantime, the runtime library watches for unsynchronized access to shared variables.

    Example of running a Go program with the race detector: go run -race main.go

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *