
Go [Golang] Interview Question and Answers [ FRESHERS ]
Last updated on 10th Nov 2021, Blog, Interview Questions
The Go programming language, or Golang, is an open-source programming language similar to C but is optimized for quick compiling, seamless concurrency, and developer ease of use.Whether you’re preparing for a Google job interview or just want to remain a cutting edge developer, Go is the right choice for you. Today, we’ll help you practice your Go skills with 50 of the most important Go questions and answers.
1. Explain something about Golang.
Ans:
Go programming language is an open-source programming language developed at Google. It is similar to C language but with some additional features such as Garbage collection, memory safety, CSP-style concurrency and structural typing. It is referred to as Golang because it’s domain is golang.org. Golang is emerged as an alternative to Java and C++ for the development of distributed systems. It is a great fit when you want to develop a small MicroService very quickly. it’s very easy to learn for developers with existing programming skills.
2. What features does Golang provide?
Ans:
- Simple and Understandable – Go language is easy to learn and simple to understand. It does not include any unnecessary feature means containing only required features. It does not matter how large is the code base but every single line is understandable and readable. Designers of Go kept simplicity, readability and maintainability its priority while developing this project.
- Powerful Standard Library – Go has all required collections of libraries or packages which makes writing the code easier. You can find all the package details here Go Standard library.
- Concurrency Support – Golang provides good support for concurrency. You can implement concurrent programming via Go Routines or Channels in an easier way than other programming languages. It takes the advantage of a multi-core processor architecture and efficient memory management.
- Static Type Checking – Go compiler makes sure that code is type safe and all type conversions are taken care of. It reduces the chances of errors at runtime.
- Binaries – Go generates binaries for your applications with all required built-in dependencies. Binaries allow non-Go users to install tools written in Go language very quickly without installing Go compiler , runtime or package manager.
- Good Testing Support – Go provides support to write unit test cases parallel with your code. You can also check code coverage and write code examples which are helpful to generate code documentation.
Go Programming language comes with some unique features which make it unique and easy to learn.
3. What basic data types does Golang provide?
Ans:
- bool
- string
- int, int8, int16, int32, int64
- uint uint8 uint16 uint32 uint64 uintptr
- byte // alias for uint8
- rune // alias for int32
- float32 float64
- complex64 complex128
Go language provides these basic data types:
4. What is the size of int (not int8 or int16)?
Ans:
If you are programming on a 32 bit system then int, uint, and uintptr are usually 32 bits wide and 64 bits wide on 64-bit systems.
5. How to find the number of CPU Cores used by the current process using Golang?
Ans:
You can use the NumCPU method of the runtime package to determine the number of processors used in the current process.
- package main
import (
“fmt”
“runtime”
)
// Main function
func main() {
// Get number of processors using the NumCPU function
fmt.Println(runtime.NumCPU())
}
6. What is rune in GoLang?
Ans:
Strings are made of bytes or characters. GoLang uses bytes to define strings and uses UTF-8 encoding standard so any valid character can be defined using “code points” in Unicode. Rune is a new term in Go Language that represents this code point. Go uses UTF-8 encoding so type int32 can be aliased as Rune.
A string can be converted to an array of runes using Rune function. Rune and byte values are the same for ASCII characters.
7.What is the use of the runes package? Explain ‘Map’ function.
Ans:
- In Go programming language, runes is a package which provides transformation functions for UTF-8 encoded text. For more about the runes package, visit runes pkg.
- Map function provides you a Transformer, which is responsible to map the runes in input string based on defined mapping. For more visit Map function.
8. Explain Map data structure in Golang.
Ans:

9. What is runtime? Explain runtime package.
Ans:
In Go language, runtime is a library used by programs at execution time. It manages the memory, garbage collector and go-routine scheduler etc. It is not a full runtime environment like CLR in C# and JVM in Java. For more about go runtime visit go runtime.
The runtime package provides you operations which interact with go runtime such as to control the garbage collector, scheduler and goroutines, etc. For more visit runtime package.
10. What is the interface? Why do we use it?
Ans:
The purpose of interfaces is to allow the computer to enforce these properties and to know that an object of TYPE T (whatever the interface is ) must have functions called X,Y,Z, etc.
It is used to achieve total abstraction. Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance . It is also used to achieve loose coupling.
11. What are rvalue and Ivalue?
Ans:
- lvalue − The expressions which are referred to as memory locations are known as “lvalue” expressions. It appears either on the right-hand or left-hand side of an assignment operator.
- rvalue − It refers to a data value that is stored at some address in memory. It cannot have a value assigned to it. So rvalues always appear on the right side of the assignment operator.
Golang supports two kinds of expressions:
12. Does Go have exceptions?
Ans:
No, Go doesn’t have exceptions. Golang’s multi-value returns make it easy to report errors without overloading the return value for plain error handling. Error-values are used to indicate an abnormal state in Go.
13. How to compare two structs?
Ans:
You can compare two structs with the “==” operator, as you would do with other types. Make sure they don’t contain any functions, maps, or slices in which the code will not be compiled.
14. What’s the difference between unbuffered and buffered channels?
Ans:
- For the buffered channel, the sender will block when there is an empty slot of the channel, while the receiver will block on the channel when it’s empty.
- Compared with the buffered counterpart, in an unbuffered channel, the sender will block the channel until the receiver receives the channel’s data. Simultaneously, the receiver will also block the channel until the sender sends data into the channel.
15. Explain how arrays in GO works differently then C ?
Ans:
- Arrays are values, assigning one array to another copies all the elements.
- If you pass an array to a function, it will receive a copy of the array, not a pointer to it
- The size of an array is part of its type. The types [10] int and [20] int are distinct
In GO Array works differently than it works in C
16. Explain GO Interfaces ?
Ans:
In GO, interfaces is a way to specify the behaviour of an object. An interface is created by using the “type” word, followed by a name and the keyword interface. An interface is specified as two things. A set of methods Also it is referred as type
17. Explain what Type assertion is used for and how it does it?
Ans:
Type conversion is used to convert dissimilar types in GO. A type assertion takes an interface value and retrieve from it a value of the specified explicit type.
18. In GO language how you can check variable type at runtime?
Ans:
A special type of switch is dedicated in GO to check variable type at runtime, this switch is referred as type switch. Also, you can switch on the type of an interface value with Type Switch.
19. How you can format a string without printing?
Ans:
- return fmt.Sprintf ( “at %v, %s” , e.When , e.What )
To format a string without printing you have to use command
20. List out the built in support in GO?
Ans:
- Container: container/list , container/heap
- Web Server: net/http
- Cryptography: Crypto/md5 , crypto/sha1
- Compression: compress/ gzip
- Database: database/sql
The available built-in-support in GO includes
21. Explain what is string literals?
Ans:
- Raw string literals: The value of raw string literals are character sequence between back quotes ‘‘. The value of a string literal is the string composed of the uninterrupted character between quotes.
- Interpreted string literals: It is represented between double quotes ““. The text between the double quotes which may not contain newlines, forms the value of the literal.
A string literals represents a string constant obtained from concatenating a sequence of characters.
There are two forms,
22. What is syntax like in GO?
Ans:
- Production = production_name “=” [ Expression ]
- Expression = Alternative { “l” Alternative }
- Alternative = Term { Term }
- Term = Production_name l token [ “…”token] l Group l Option l Repetition
- Group = “ ( “ Expression”)”
- Option = “ [ “ Expression “ ]”
- Repetition = “ {“ Expression “}”
Syntax in GO is specified using Extended Backus-Naur Form (EBNF)
23. Explain packages in Go program?
Ans:
Every GO program is made up of packages. The program starts running in package main. This program is using the packages with import paths “fmt” and “math/rand”.
24. Explain workspace in GO?
Ans:
- src contains GO source files organized into packages
- pkg contains package objects and
- bin contains executable commands
Inside a workspace GO code must be kept. A workspace is a directory hierarchy with three directories at its root.
25. Explain what is GOPATH environment variable?
Ans:
The GOPATH environment variable determines the location of the workspace. It is the only environment variable that you have to set when developing Go code.
26. Explain what is string types?
Ans:
Public is the default visibility for properties/methods in TypeScript classes.
27. What are the advantages of GO?
Ans:
- GO compiles very quickly
- Go supports concurrency at the language level
- Functions are first class objects in GO
- GO has garbage collection
- Strings and Maps are built into the language
28.What are the different methods in Go programming language?
Ans:
In Go programming language there are several different types of functions called methods. In method declaration syntax, a “receiver” is used to to represent the container of the function. This receiver can be used to call function using “.” operator.
29. What is goroutine?
Ans:
It is a function that runs concurrently with other functions. If you want to stop it, you will have to pass a signal channel to the goroutine, which will push a value into when you want the function to finish.
30. What is meant by L value and R value in Golang?
Ans:
- Shows on assignment operator’s right side.
- Always assigned to lvalue.
- Shows on assignment operator’s left side.
- Designated to a variable and not a constant.
Rvalue
Lvalue
31. How will you access command line arguments in a GO program?
Ans:
- For instance:
- Package main
- import (
- “fmt”
- “OS”
- )
- func main () {
- fmt.Println(len(os.Args), os.Args)
The command line argument can be accessed using the os.Args variables.
32. What is CGO Golang?
Ans:
In Golang, the Cgo lets all the Go packages call a C code. With a Go source file written on some special features, the cgo makes an output in Go and C files which can be then combined into a single Go package bundle.
33. Explain Singleton Design Pattern in Golang?
Ans:

34. What is Shadowing?
Ans:
In Golang, a shadowed variable is one which is declared in an inner scope having the same name and type as a variable in the outer scope. Here, the outer variable is mentioned after the inner variable is declared.
35. How is testing performed in GO?
Ans:
- Create a file and end it with _test.go.
- This file should contain the TestXxx functions as described.
- Now, put this file in the same package as the one which is being tested.
- This file will now be included in the “go test” command.
To test on Golang, follow these steps:
36. Explain what is GOPATH environment variable?
Ans:
The GOPATH environment variable determines the location of the workspace. It is the only environment variable that you have to set when developing Go code.
37. What is goroutine in Go programming language?
Ans:
- Quit : = make (chan bool)
- go func ( ) {
- for {
- select {
- case <- quit:
- return
- default
- // do other stuff
- }
- }
- }()
- // Do stuff
- // Quit goroutine
- Quit <- true
A goroutine is a function which usually runs concurrently with other functions. If you want to stop goroutine, you pass a signal channel to the goroutine, that signal channel pushes a value into when you want the goroutine to stop.
The goroutine polls that channel regularly as soon as it detects a signal, it quits.
38. What are channels and how can you use them in Golang?
Ans:
In Golang, a channel is a communication object which uses goroutines to communicate with each other. Technically, it is a data transfer pipe in which data can be transferred into or read from.
39. What is the difference between Gopath and Goroot?
Ans:
This must be set in order to get, develop and install packages outside the standard Golang tree. | Must be set only when installing to a specific custom location. |
40. What is the usage of break statement, continue statement and goto statement?
Ans:
Break statement: It terminates the “for” loop or switch statement and transfers execution following the “for” loop or switch.
Continue statement: It helps the loop to omit the remainder of its body and retest before repeating. Goto statement: It transfers control to the statement41. What are nil Pointers?
Ans:
When a pointer is assigned “nil”, it called a nil pointer. It is a constant with a “zero” value defined in standard libraries.
42. How is actual parameter different from the formal parameter?
Ans:
Actual parameters: Parameters that are sent to the function at the calling end.
Formal Parameters: Parameters that are at the receiving of the function definition.
43. What data types does Golang use?
Ans:
- Method
- Boolean
- Numeric
- String
- Array
- Slice
- Struct
- Pointer
- Function
- Interface
- Map
- Channel
Golang uses the following types:
44. What are the most important influences of earlier programming languages on the design of Go?
Ans:

45. What is slice?
Ans:
Go Array allows programmers to define factors that can hold information of a similar kind yet not give any strategy for building size or for getting a sub-exhibit. Slice takes care of this limitation. It provides utility functions needed on Array and is a part of Go programming.
46. How many ways we can pass parameters to a function in GO Programming?
Ans:
- Call by value
- Call by reference
47. What are function closures?
Ans:
Function closures is a function value that references variables from outside its body. The function may access and assign values to the referenced variables.
For example: adder() returns a closure, which is each bound to its own referenced sum variable.
48. What are the looping constructs in Go?
Ans:
- The Init statement, which is executed before the loop begins. It’s often a variable declaration only visible within the scope of the for loop.
- The condition expression, which is evaluated as a Boolean before each iteration to determine if the loop should continue.
- The post statement, which is executed at the end of each iteration.
Go has only one looping construct: the for loop. The for loop has 3 components separated by semicolons:

49. Explain the difference between concurrent and parallelism in Golang?
Ans:
Concurrency is when your program can handle multiple tasks at once while parallelism is when your program can execute multiple tasks at once using multiple processors. | Parallelism can therefore be a means to achieve the property of concurrency, but it is just one of many means available to you. |
concurrency is a property of a program that allows you to have multiple tasks in progress at the same time, but not necessarily executing at the same time. | Parallelism is a runtime property where two or more tasks are executed at the same time. |
50. Is Go front end or backend?
Ans:
The Go code can be run using the goper. js in the browser. But the fact is that most of the developers give priority to the JavaScript front-end programming languages for client-side development. Go is preferred more as the backend language and it offers high performance for developing the cocurrent applications
51. What is defer in Golang?
Ans:
In Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns. In other words, defer function or method call arguments evaluate instantly, but they don’t execute until the nearby functions returns
52. Can we create complex number from two float in Golang?
Ans:
- Complex64
- Complex128
A complex number is a basic data type of GoLang. It is a combination of the real and imaginary part, where both parts of the complex number will be float type in the go program. Complex numbers are further categorized into two parts in go language. These two parts are:
53. Explain the differences between Goroutine and Thread?
Ans:
Goroutine are not hardware dependent. | Threads are hardware dependent. |
Goroutines have easy communication medium known as channel. | Thread doesnot have easy communication medium. |
Due to the presence of channel one goroutine can communicate with other goroutine with low latency. | Due to lack of easy communication medium inter-threads communicate takes place with high latency. |
Goroutine doesnot have ID because go doesnot have Thread Local Storage. | Threads have their own unique ID because they have Thread Local Storage. |
Goroutines are cheaper than threads. | The cost of threads are higher than goroutine. |
They have fasted startup time than threads. | They have slow startup time than goroutines. |
Goroutine has growable segmented stacks. | Threads doesnot have growable segmented stacks. |
54. Does Go have async await?
Ans:
To declare an “async” function in Go: The return type is <-chan ReturnType . Within the function, create a channel by make(chan ReturnType) and return the created channel at the end of the function. Return the result by sending the value to channel.
55.What is meant by multithreading?
Ans:
Multithreading is a model of program execution that allows for multiple threads to be created within a process, executing independently but concurrently sharing process resources. Depending on the hardware, threads can run fully parallel if they are distributed to their own CPU core.
56. Does Go support multithreading?
Ans:
The spec defines them as “an independent concurrent thread of control within the same address space”. The go runtime is capable of handling thousands or even hundreds of thousands of goroutines without a problem.
57. What projects are written in Golang?
Ans:
- Kubernetes.
- Hugo.
- CoreDNS.
- CockroachDB.
- Monzo Bank.
58. Why is Docker written in Go?
Ans:
The reason is that Go is one of the efficient programming low-level languages which can handle high-level tasks, Docker requires fewer system resources to run it and Go build time is too less comparatively with other programming languages.
59. Why is Go used for containers?
Ans:
For the developer, Go makes it easy to package pieces of code functionality, and then build applications by assembling these packages. The packages can then be easily reused for other applications as well. And just as Go helps in development, it also makes life easier during deployment.
60. What are the parametric curves produced by harmonic oscillation in two dimensions?
Ans:

61. Can constants be computed in Go?
Ans:
Constants can not be computed at runtime, their value must be known at compile time. That said, constants can be computed at compile-time, typically as a derivative of other constants.
62. Does Go support generic programming?
Ans:
Go doesn’t currently support generics, but support is coming soon. Generics will allow us as developers to write code that operates on types without needing to re-implement the same function multiple times for all the different types.
For example, say you want to implement a Binary Tree in Go, that can store any type of item. This should be easy, because the binary tree shouldn’t care about the types it stores, it never needs direct access to them. Currently, you only have two options: rewrite the tree for every type you want to support, or use an interface and cast the type every time you insert or remove something from the tree, neither of which are ideal solutions.
63. Give an example of an array and slice declaration.
Ans:
- friends := [4]string{“Dan”, “Diana”, “Paul”, “John”}
- numbers := []int{2, 3, 4, 5}
Here’s an example of declaring and initializing an array of type [4] string using the short declaration syntax.
Here’s an example of declaring and initializing a slice of type [] int using the short declaration syntax.
64. How can we swap two numbers in Go?
Ans:
- package main
- import “fmt”
- func main() {
- fmt.Println(myfunction())
- }
- func myfunction() []int {
- a, b := 10, 5
- b, a = a, b
- return []int{a, b} }
- func swap(sw []int){
- for a,b:=0, len(sw)-1; a
- sw[a], sw[b] = sw[b],sw[a]
- }}
- func main(){
- x:=[]int{3,2,1}
- swap(x)
- fmt.Println(x)
- //Output: [1,2,3]
- }
Similarly,
65. What is the GOPATH variable in Golang?
Ans:
The GOPATH environment variable is employed to symbolized directories out of $GOROOT that combines the source for Go projects including their binaries.
66. What is the difference between actual and formal parameters?
Ans:
Goroutines are managed by the go runtime. | Operating system threads are managed by kernal.|
When a function is called, the values (expressions) that are passed in the function call are called the arguments or actual parameters. | The parameter used in function definition statement which contain data type on its time of declaration is called formal parameter. |
These are the variables or expressions referenced in the parameter list of a subprogram call. | These are the variables or expressions referenced in the parameter list of a subprogram specification. |
Actual Parameters are the parameters which are in calling subprogram. | Formal Parameters are the parameters which are in called subprogram. |
There is no need to specify datatype in actual parameter. | The datatype of the receiving value must be defined. |
The parameters are written in function call are known as actual parameters. | The parameters are written in function definition are known as formal parameters. |
Actual Parameters can be constant values or variable names. | Formal Parameters can be treated as local variables of a function in which they are used in the function header. |
67. How will you document libraries?
Ans:
Godoc extracts package documentation from the source code that can be utilized on the command line or the web. An instance is golang.org/pkg/.
68. What is a unique benefit of Go’s compiler?
Ans:
Go has the ability to cross-compile your application to run on a different machine than the one used for development. The Go compiler allows you to generate executable binaries for different operating systems with simple commands.
69. Name one Go feature that would be helpful for DevOps.
Ans:
Go’s inherent features help enable the best practices for DevOps: immutable packages, testing, build promotion, security, etc. GoCenter is also a great resource for Go developers, providing always-available immutable access to dependencies with important metrics.
70. Why is Go often called a “Post-OOP” language?
Ans:
Go (or “Golang”) is a post-OOP programming language that borrows its structure (packages, types, functions) from the Algol/Pascal/Modula language family. Nevertheless, in Go, object-oriented patterns are still useful for structuring a program in a clear and understandable way.
71. What is the difference between C arrays and Go slices?
Ans:
Unlike arrays, slices can be resized using the built-in append function. Further, slices are reference types, meaning that they are cheap to assign and can be passed to other functions without having to create a new copy of its underlying array.
72. How does Go handle dependencies?
Ans:
In Go, you manage dependencies as modules that contain the packages you import. This process is supported by: A decentralized system for publishing modules and retrieving their code. Developers make their modules available for other developers to use from their own repository and publish with a version number.
73. List the operators in Golang?
Ans:
- Arithmetic operators
- Bitwise operators
- Relational operators
- Logical operators
- Assignment operators
- Misc operators
74.Why was Golang developed?
Ans:
- Dynamically typed language and interpreted language
- Compiled language and the safety and efficiency of statistically typed
- To be fast in the compilation
- To support the multi-core computing
Golang is developed out of the difficulty in existing environments and languages for system programming.
Go is an effort to have:
75. Why should one learn the Golang programming language?
Ans:
- Easy to learn: The syntax of the Golang programming language is similar to that of C, and it’s easy to pick up for C or Java programmers. Compared to other programming languages, Golang’s syntax is smaller than others and easy to read and write programs.
- Concurrency: Creating multithreading applications using the Golang language is easy.
- Networking: Go is excellent for writing networking applications like TCP or HTTP servers at the production level. Go supports parsing libraries that are easy to plug into other services.
- Tools: As Golang is open-source, there are large development tools that are already present.
- Speedy execution: compared to other programming languages, Go’s language execution is very fast.
There are various reasons why one should learn the Golang programming language. Let’s see one by one:
76. Does Golang copy append?
Ans:
Otherwise, append re-uses the underlying array. The function copy copies slice elements from a source src to a destination dst and returns the number of elements copied. Both arguments must have identical element type T and must be assignable to a slice of type.
77. How do you concatenate bytes in Golang?
Ans:
In the Go slice of bytes, you are allowed to join the elements of the byte slice with the help of Join() function. Or in other words, Join function is used to concatenate the elements of the slice and return a new slice of bytes which contain all these joined elements separated by the given separator.
78. What are the features of Golang?
Ans:

79. What is shadowing?
Ans:
In Golang, a shadowed variable is declared in an inner scope with the same name and type as a variable in the outer scope. Here, the outer variable is mentioned after the inner variable is declared.
80. When Should Panic Be Used?
Ans:
There are two valid use cases for panic.
1.An unrecoverable error where the program cannot simply continue its execution.
One example is a web server that fails to bind to the required port. In this case, it’s reasonable to panic as there is nothing else to do if the port binding itself fails.
2.A programmer error.
Let’s say we have a method that accepts a pointer as a parameter and someone calls this method using a nil argument. In this case, we can panic as it’s a programmer error to call a method with nil argument which was expecting a valid pointer.
81. Is Go functional or object-oriented?
Ans:
Go is a Post-OOP programming language that borrows the structure (functions, packages, types) from the Pascal/Algol/Modula language family. In Go, object-oriented patterns are still helpful to structure a program in a clear and understandable way.
82. Explain about switch statement in Golang?
Ans:
A switch statement is a multi-way branch record. It gives an effective way to assign the execution to various parts of code based upon the utility of the expression. Go language holds two types of switch statements they are
Expression switch: expression switch in Golang is the same as the switch statement in C, C++, Java languages, It gives an effortless way to dispatch execution to various parts of code which is based upon the value of the phrase.
83. What is the use of structs in Golang?
Ans:
A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-oriented programming.
84. Why is Type assertion used?
Ans:
It is used to check values that are held by interface type variable. It is also used to convert various GO types.
85. How can you compile a Go program for Windows and Mac?
Ans:
- GOOS=windows GOARCH=amd64 go build -o my_go_program.exe
- GOOS=darwin GOARCH=amd64 go build -o my_go_program
To compile the program, move to the application’s directory. Then execute the following commands in the terminal.
Compile the application for Windows and 64bit CPUs:
Compile the application for Mac and 64bit CPUs:
86. How can you format the Go source code in an idiomatic way?
Ans:
The Golang standards do not insist on a coding style as Python does, for example. Instead, the standards do recommend certain styles.
gofmt (golang formatter) will format a program’s source code in an idiomatic way. This makes it easy to read and understand. This tool is built into the language runtime, and it formats the Go source code according to a set of stable, well-understood language rules.
We can run it by typing the command at the command line or we can configure the IDE to run gofmt each time we save a file.
For example, gofmt -w main.go will format the source file main.go according to Go Programming Language style recommendations. The -w option writes the results back to the source file.
If we want to run gofmt on all files in a specific directory, we run: gofmt -w -l directory
You can also run the go command with the fmt argument. It will execute goftm -l -w behind the scenes and format all source code in each file in the current working directory.
87. How To Delete An Entry From A Map In Go?
Ans:
delete() function is used to delete an entry from the map. It requires map and corresponding key which is to be deleted.
88. Consider the following code. What will be the value of s1?
Ans:
- primes := [6]int{2, 3, 5, 7, 11, 13}
- s1 := primes[1:4]
s1 will be: [3 5 7]
When slicing an existing array or slice the first index is inclusive while the last index is exclusive. If an index is omitted on one side of the colon, then all values until the edge of the original slice are included in the result.
89. Describe the difference between sync.Mutex and sync.RWMutex
Ans:
The normal mutex locks data so that only one goroutine can access it at a time.
A RWMutex (read/write) can lock data for “reading” and for “writing”. When locked for reading, other readers can also lock and access data. When locked for writing no other goroutines, including other readers can access the data.
90. What is a pointer and when would you use it?
Ans:
- & generates a pointer to its operand.
- dereferences a pointer (exposes the underlying value).
- Allow a function to directly mutate a value that is passed to it
- To increase performance in edge cases. Sometimes passing a large struct of data results in inefficient copying of data
- To signify the lack of a value. For example, when unmarshalling JSON data into a struct it can be useful to know if a key was absent rather than it being present with the zero value.
A pointer holds the memory address of a value.
Pointers can be used to:
91. What is a select statement in Golang?
Ans:
In Go language, the select statement is just similar to a switch statement, however, in the select statement, the case statement indicates communication, i.e. sent or receive progress on the channel.