Go Programming Interview Questions -Top Answers to Go Interview Questions. Learn Go programming language with simple and easy examples.
Go is a general-purpose programming language with a clean syntax and advanced features, including concurrency. It is a created at Google in 2009 by Robert Griesemer, Rob Pike, and Ken Thompson that aims to simplify systems programming and to make programming fun again. It is a compiled, statically typed language in the tradition of Algol and C, with garbage collection, limited structural typing, memory safety features and CSP-style concurrent programming features added. The compiler and other language tools originally developed by Google are all free and open source.
Go programming is not what you think it is. It is not Java and Ruby. It is not trying to be a language theorist’s dream. Go is not about type hierarchies. It is not about object-oriented programming or functional programming. Honestly, it has little to recommend it if you’re looking for something fashionable with all the buzzwords attached. Go’s purpose is therefore not to do research into programming language design; it is to improve the working environment for its designers and their coworkers. Go is more about software engineering than programming language research. Or to rephrase, it is about language design in the service of software engineering. (Reference: Rob pike)
Go is a compiled programming language, which means source code is translated into a language that your computer can understand. Therefore before we can write a Go program, we need the Go compiler.
The language was announced in November 2009. It is used in some of Google's production systems, as well as by other firms. Two major implementations exist:
  • Google's Go compiler, "gc", developed as open-source software. It targets various platforms including Linux, OS X, Windows, various BSD and UNIX versions, and also (since 2015) mobile devices (including smartphones).
  • A second compiler, “gccgo”, is a GCC frontend.
Google released Go 1.0 in March 2012. Each major Go release is supported until there are two newer major releases. For example, Go 1.8 is supported until Go 1.10 is released, and Go 1.9 is supported until Go 1.11 is released. Iit fix critical problems, including critical security problems, in supported releases as needed by issuing minor revisions (for example, Go 1.9.1, Go 1.9.2, and so on). Go 1.9 is a major release of Go .Current release is Go 1.9.2 / October 25, 2017.
What is “Go” programming?
Go is an open source or general purpose programming language with advanced more features and easy understanding small syntax. Go is also a great language for writing concurrent programs. Programs with many independently running parts. Go is deliberately not an object-oriented language: There are no classes, objects, or inheritance. Instead, we’ll declare a struct type called Fund, with a simple function to create new fund structs, and two public methods.

Why “Go” programming?
Go programming language has different features that make it attractive and special. Here is a not limited list:
  • Go programming has the good tooling and a great standard library that helps you build robust programs. The standard Go library is very complete. Almost everything has a built-in package. HTTP servers to clients, cryptography, testing, managing file systems, and more.
  • Go programming is fast. And not only fast in the sense that programs written in it run fast when compared to other common languages; but also fast in the sense that its compiler can compile projects in the blink of an eye. So much so that it makes Go feels like an interpreted language instead of a compiled one. You can even edit and run Go programs directly on the Web.
  • Go has a rich standard library which covers a lot of areas. In fact, Go is probably the only language that can claim to have a fully working Web server as part of its standard library.
  • Although it is compiled, Go is also a garbage-collected language. This puts less pressure on the developer to do memory management, as the language itself takes care of most of the grunt work needed. In this respect, it is similar to Java, as opposed to C++.
  • Go has built-in concurrency, which allows parallelism in an easier way than is possible in other languages. Go has the concept of goroutines to start concurrent work and the concept of channels to permit both communication and synchronization.
  • Go is a statically-typed language (which makes code more robust as several possible bugs can be detected during compilation) that supports duck-typing (types can satisfy interfaces and be passed in to any functions/methods that accept those interfaces). Go also has type inference, which means you do not need to explicitly mention a variable type whenever it can be inferred by the compiler.
  • Go’s built-in build system is both elegant and simple. No need to mess with build configurations or make files.
What is syntax of go programming? Or Hello world program in Go?
First create a new folder where we can store our program. Create a folder named ~/src/golang-notes/section 1. (Where ~ means your home directory) From the terminal you can do this by entering the following commands:
mkdir src/golang-notes
mkdir src/golang-notes/section 1
Using your text editor type in the following:
package main
import "fmt"
// this is a comment
func main() {
    fmt.Println("Hello World")
}
Make sure your file is identical to what is shown here and save it as main.go in the folder we just created. Open up a new terminal and type in the following:
cd src/golang-notes/section 1
go run main.go
You should see Hello World displayed in your terminal.

Does Go programming support type inheritance and Overloading and method overloading and arithmetic’s and generic programming?
Go programming language doesn't support or No support for type inheritance, Overloading, method overloading, arithmetic’s and generic programming.

What is workspace in Go programming?
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
What are the lvalues and the rvalues in Go programming?
There are two kinds of expressions in Go:
lvalue: Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.
rvalue: The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment.

What is variable in Go programming?
A variable definition tells the compiler where and how much storage to create for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type as follows: var variable_list optional_data_type; Here, optional_data_type is a valid Go data type including byte, int, float32, complex64, Boolean or any user-defined object, etc.,

What is a string literal in Go?
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.

What is static type declaration and dynamic type declaration of a variable in Go?
Static type declaration: Static type variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable declaration at the time of linking of the program.
Dynamic type declaration: A dynamic type variable declaration requires compiler to interpret the type of variable based on value passed to it. Compiler don't need a variable to have type statically as a necessary requirement.

What are types in go programming?
The following are the basic types available in go
Boolean types: A Boolean value (named after George Boole) is a special 1 bit integer type used to represent true and false. Three logical operators are used with Boolean values:
  • || (or)
  • && (and)
  • ! (not)
Numbers types: Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
  • int8, int16, int32, int64, int
  • uint8, uint16, uint32, uint64, uint
  • float32, float64
  • complex64, complex128
  • byte
  • rune
Strings types: Go strings are made up of individual bytes, usually one for each character. String literals can be created using double quotes "Hello World" or back ticks `Hello World`. The difference between these is that double quoted strings cannot contain newlines and they allow special escape sequences. For example \n gets replaced with a newline and \t gets replaced with a tab character.
Derived types: They include Pointer types, Array types, Structure types, Union types, Slice types, Interface types, Map types, and Channel Types.

What is a pointer in Go programming?
A pointer is used to hold the address of a variable.

What is the purpose of break statement?
Break terminates the for loop or switch statement and transfers execution to the statement immediately following the for loop or switch.

What is a structure in go?
A structure is a type which contains named fields. Or a structure is a user defined type which represents a collection of fields. Structures are used to represent a record. It can be used in places where it makes sense to group the data into a single unit rather than maintaining each of them as separate types.

What is an interface in go?
"Interface defines the behavior of an object". In Go, an interface is a set of method signatures. When a type provides definition for all the methods in the interface, it is said to implement the interface. It is much similar to the OOP world. Interface specifies what methods a type should have and the type decides how to implement these methods.

What is testing in go?
Go includes a special program that makes writing tests easier. The go test command will look for any tests in any of the files in the current folder and run them. Tests are identified by starting a function with the word Test and taking one argument of type *testing.T.

What is a function in go programming?
A function is a block of code that performs a specific task. A function takes a input, performs some calculations on the input and generates a output.
Function declaration:
The general syntax for declaring a function in go is
func functionname(parametername type) returntype { 
 //function body
}

What is Modular Programming?
Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions.

What is a token?
A Go program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

What is a nil Pointers in Go?
Go language 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.

What is panic? When should panic be used?
The idiomatic way to handle abnormal conditions in a program in Go is using errors. Errors are sufficient for most of the abnormal conditions arising in the program. One important factor is that you should avoid panic and recover and use errors where ever possible. Only in cases where the program just cannot continue execution should a panic and recover mechanism be used.
There are two valid use cases for panic.
  • An unrecoverable error where the program cannot simply continue its execution.One example would be a web server which 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.
  • A programmer error. Let's say we have a method which accepts a pointer as a parameter and someone calls this method using nil as 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.
How to delete an entry from a map in Go programming?
The delete () function is used to delete an entry from the map and it’s requires map and corresponding key which is to be deleted.

What is Defer in go?
Defer statement is used to execute a function call just before the function where the defer statement is present returns. Defer is not restricted only to functions. It is perfectly legal to defer a method call too. Defer is used in places where a function call should be executed irrespective of the code flow.

What is slice in Go programming?
A slice is a convenient, flexible and powerful wrapper on top of an array. Slices do not own any data on their own. They are the just references to existing arrays. (Or)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.

What is channels?
Channels can be thought as pipes using which Goroutines communicate. Similar to how water flows from one end to another in a pipe, data can be sent from one end and received from another end using channels.

What is range in Go programming?
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair.

What is maps in Go programming?
A map is a built-in type in Go which associates a value to a key. The value can be retrieved using the corresponding key. Go provides another important data type map which maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After value is stored, you can retrieve it by using its key.

What is recursion?
Recursion is the process of repeating items in a self-similar way. The same concept applies in programming languages as well. If a program allows to call a function inside the same function, then it is called a recursive function call

What is the difference between cap () and len () in go?
Len (): Ggives the number of elements inside a slice.
Cap (): Gives the capacity of the slice. Number of elements the slice can accommodate.

What is Type casting in go programming?
Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another using the cast operator. Its syntax is as follows :
type_name(expression)

What is Goroutin?
Goroutin is a function or method that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread. Hence it’s common for Go applications to have thousands of Goroutines running concurrently. Goroutines communicate using channels. Channels by design prevent race conditions from happening when accessing shared memory using Goroutines.

What is range in go programming?
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair. Range either returns one value or two. If only one value is used on the left of a range expression, it is the 1st value in the following table.
Range expression     1st Value       2nd Value (Optional)
Array or slice a [n]E  index i int     a[i] E
String s string type   index i int     rune int
map m map[K]V       key k K         value m[k] V
channel c chan E      element e E   none

What is Gopath?
The GOPATH environment variable is used to specify directories outside of $GOROOT that contain the source for Go projects and their binaries.

Why won't $GOPATH/src/cmd/mycmd/*.go build in go programming?
Go command is looking for packages, it always looks in $GOROOT first. This includes directories, so if it finds (as in the case above) a cmd/ directory in $GOROOT it won't proceed to look in any of the GOPATH directories. This prevents you from defining your own math/matrix package as well as your own cmd/mycmd commands.

What are the different built-in supports in Go programming?
There are built-in supports in Go programming:
  • Database: database/sql
  • Container: container/list, container/heap
  • Web Server: net/http
  • Cryptography: Crypto/md5, crypto/sha1
  • Compression: compress/ gzip
Request to Download PDF

Post A Comment:

0 comments: