Top Interview Questions & Answers | Learn Now

Go Interview Questions and Answers | Basic and Advanced Levels

Written by Mohammed | May 21, 2018 5:53:52 AM

Q1. What is Go?

Ans: Go is a general-purpose language designed with systems programming in mind.It was initially developed at Google in year 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is strongly and statically typed, provides inbuilt support for garbage collection and supports concurrent programming. Programs are constructed using packages, for efficient management of dependencies. Go programming implementations use a traditional compile and link model to generate executable binaries.

Q2. What are the advantages/ benefits of Go programming language?

Ans:  Advantages/ Benefits of Go programming language:

  • Go is fast and compiles very quickly.
  • It supports concurrency at the language level.
  • It has Garbage collection.
  • It supports various safety features and CSP-style concurrent programming features.
  • Strings and Maps are built into the language.
  • Functions are first class objects in this language.

Q3. What are Go's ancestors? 

Ans: Go is mostly in the C family (basic syntax), with significant input from the Pascal/Modula/Oberon family (declarations, packages), plus some ideas from languages inspired by Tony Hoare's CSP, such as Newsqueak and Limbo (concurrency). However, it is a new language across the board. In every respect the language was designed by thinking about what programmers do and how to make programming, at least the kind of programming we do, more effective, which means more fun.

Q4. Is Go an object-oriented language?

Ans: Yes and no. Although Go has types and methods and allows an object-oriented style of programming, there is no type hierarchy. The concept of “interface” in Go provides a different approach that we believe is easy to use and in some ways more general. There are also ways to embed types in other types to provide something analogous—but not identical—to subclassing. Moreover, methods in Go are more general than in C++ or Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).
Also, the lack of a type hierarchy makes “objects” in Go feel much more lightweight than in languages such as C++ or Java.

Q5. Does Go have a runtime?

Ans: Go does have an extensive library, called the runtime, that is part of every Go program. The runtime library implements garbage collection, concurrency, stack management, and other critical features of the Go language. Although it is more central to the language, Go's runtime is analogous to libc, the C library.
It is important to understand, however, that Go's runtime does not include a virtual machine, such as is provided by the Java runtime. Go programs are compiled ahead of time to native machine code. Thus, although the term is often used to describe the virtual environment in which a program runs, in Go the word “runtime” is just the name given to the library providing critical language services.

Q6. Does Go support operator overloading?

Ans: No support for operator overloading.

Q7. Does Go support method overloading?

Ans: No support for method overloading.

Q8.Does Go support pointer arithmetics?

Ans: No support for pointer arithmetic.

Q9. Does Go support generic programming?

Ans: No support for generic programming.

Q10. Is Go a case sensitive language?

Ans: Yes! Go is a case sensitive programming language.

Q11. What is a nil Pointers in Go?

Ans: Go compiler assign a Nil value to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned nil is called a nil pointer. The nil pointer is a constant with a value of zero defined in several standard libraries.

Q12. What is a string literal in Go programming?

Ans: A string literals specifies a string constant that is obtained from concatenating a sequence of characters.
There are two types of string literals:

  • Raw string literals: The value of raw string literals are character sequence between back quotes ". Its value is specified as a string literal that composed of the uninterrupted character between quotes.
  • Interpreted string literals: It is shown between double quotes " ". The value of the literal is specified as text between the double quotes which may not contain newlines.

Q13. What is workspace in Go?

Ans: A workspace contains Go code. A workspace is a directory hierarchy with three directories at its root.

  • "src" directory contains GO source files organized into packages.
  • "pkg" directory contains package objects.
  • "bin" directory contains executable commands

Q14. Why goroutines instead of threads?

Ans: Goroutines are part of making concurrency easy to use. The idea, which has been around for a while, is to multiplex independently executing functions—coroutines—onto a set of threads. When a coroutine blocks, such as by calling a blocking system call, the run-time automatically moves other coroutines on the same operating system thread to a different, runnable thread so they won't be blocked. The programmer sees none of this, which is the point. The result, which we call goroutines, can be very cheap: they have little overhead beyond the memory for the stack, which is just a few kilobytes.
To make the stacks small, Go's run-time uses resizable, bounded stacks. A newly minted goroutine is given a few kilobytes, which is almost always enough. When it isn't, the run-time grows (and shrinks) the memory for storing the stack automatically, allowing many goroutines to live in a modest amount of memory. The CPU overhead averages about three cheap instructions per function call. It is practical to create hundreds of thousands of goroutines in the same address space. If goroutines were just threads, system resources would run out at a much smaller number.

 

Q15. What is structure in Go?

Ans: Structure is another user defined data type available in Go programming, which allows you to combine data items of different kinds.

Q16. How to define a structure in Go?

Ans: To define a structure, you must use type and struct statements. The struct statement defines a new data type, with more than one member for your program. type statement binds a name with the type which is struct in our case.
The format of the struct statement is this:

type struct_variable_type struct {
member definition;
member definition;

member definition;
}

Q17. How are libraries documented?

Ans: There is a program, godoc, written in Go, that extracts package documentation from the source code. It can be used on the command line or on the web. An instance is running at golang.org/pkg/. In fact, godoc implements the full site at golang.org/.

A godoc instance may be configured to provide rich, interactive static analyses of symbols in the programs it displays; details are listed here.
For access to documentation from the command line, the go tool has a doc subcommand that provides a textual interface to the same information

Q18. What is lvalue and rvalue?

Ans: The expression appearing on right side of the assignment operator is called as rvalue. Rvalue is assigned to lvalue, which appears on left side of the assignment operator. The lvalue should designate to a variable not a constant.

Q19. What is the difference between actual and formal parameters?

Ans: The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.

Q20. What is slice in Go?

Ans: Go Slice is an abstraction over Go Array. As Go Array allows you to define type of variables that can hold several data items of the same kind but it do not provide any inbuilt method to increase size of it dynamically or get a sub-array of its own. Slices covers this limitation. It provides many utility functions required on Array and is widely used in Go programming.

Q21. In how many ways you can pass parameters to a method?

Ans: While calling a function, there are two ways that arguments can be passed to a function −
Call by value − This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
Call by reference − This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

Q22. What's the difference between new and make?

Ans: In short: new allocates memory, make initializes the slice, map, and channel types.

Q23. What is the size of an int on a 64 bit machine?

Ans: The sizes of int and uint are implementation- specific but the same as each other on a given platform. For portability, code that relies on a particular size of value should use an explicitly sized type, like int64. Prior to Go 1.1, the 64-bit Go compilers (both gc and gccgo) used a 32-bit representation for int. As of Go 1.1 they use a 64-bit representation.
On the other hand, floating-point scalars and complex types are always sized (there are no float or complex basic types), because programmers should be aware of precision when using floating-point numbers. The default type used for an (untyped) floating-point constant is float64. Thus foo := 3.0 declares a variable foo of type float64. For a float32 variable initialized by an (untyped) constant, the variable type must be specified explicitly in the variable declaration:

var foo float32 = 3.0

Alternatively, the constant must be given a type with a conversion as in foo := float32(3.0).

Q24. Why does my Go process use so much virtual memory?

Ans: The Go memory allocator reserves a large region of virtual memory as an arena for allocations. This virtual memory is local to the specific Go process; the reservation does not deprive other processes of memory.
To find the amount of actual memory allocated to a Go process, use the Unix top command and consult the RES (Linux) or RSIZE (Mac OS X) columns.

Q25. Why are ++ and -- statements and not expressions? And why postfix, not prefix?

Ans: Without pointer arithmetic, the convenience value of pre- and postfix increment operators drops. By removing them from the expression hierarchy altogether, expression syntax is simplified and the messy issues around order of evaluation of ++ and -- (consider fi(++) and p[i] = q[++i]) are eliminated as well. The simplification is significant. As for postfix vs. prefix, either would work fine but the postfix version is more traditional; insistence on prefix arose with the STL, a library for a language whose name contains, ironically, a postfix increment.