Go Interview Questions

  • 1
     Explain something about Golang.

    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 basic data types does Golang provide?

    Go language provides these basic data types:

    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
    
  • 3
    How to find the number of CPU Cores used by the current process using Golang?

    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()) }
  • 4
    What is the use of the runes package? Explain ‘Map’ function.

    • 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.
  • 5
    What is runtime? Explain runtime package.

    • 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.
  • 6
    What are rvalue and Ivalue?

    Golang supports two kinds of expressions:

    • 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.
  • 7
    How to compare two structs?

    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.

  • 8
     Explain how arrays in GO works differently then C ?

    In GO Array works differently than it works in C

    • 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
  • 9
    Explain what Type assertion is used for and how it does it?

    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.

  • 10
    How you can format a string without printing?

    To format a string without printing you have to use command

    return fmt.Sprintf ( “at %v, %s” , e.When , e.What )
    
  • 11
     Explain what is string literals?

    A string literals represents a string constant obtained from concatenating a sequence of characters.

    • There are two forms,
    • 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.
  • 12
    Explain packages in Go program?

    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”.

  • 13
    Explain what is GOPATH environment variable?

    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.

  • 14
    What are the advantages of GO?

    • 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
  • 15
    What is goroutine?

    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.

  • 16
    How will you access command line arguments in a GO program?

    The command line argument can be accessed using the os.Args variables.

    For instance:
    Package main
    import (
    “fmt”
    “OS”
    )
    func main () {
    fmt.Println(len(os.Args), os.Args)
    
  • 17
    How is testing performed in GO?

    To test on Golang, follow these steps:

    • 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.
  • 18
    What is goroutine in Go programming language?

    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.

    Quit : = make (chan bool)
    go func ( ) {
    for {
    select {
    case <- quit:
    return
    default
    // do other stuff
    }
    }
    }()
    // Do stuff
    // Quit goroutine
    Quit <- true
    
  • 19
    What are nil Pointers?

    When a pointer is assigned “nil”, it called a nil pointer. It is a constant with a “zero” value defined in standard libraries.

  • 20
     What data types does Golang use?

    Golang uses the following types:

    • Method
    • Boolean
    • Numeric
    • String
    • Array
    • Slice
    • Struct
    • Pointer
    • Function
    • Interface
    • Map
    • Channel
  • 21
    What is slice?

    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.

  • 22
    What are function closures?

    Function closures is a function value that references variables from outside its body. The function may access and assign values to the referenced variables.

    example: adder() returns a closure, which is each bound to its own referenced sum variable.

  • 23
    What is defer in Golang?

    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

  • 24
    What is meant by multithreading?

    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.

  • 25
    What projects are written in Golang?

    • Kubernetes.
    • Hugo.
    • CoreDNS.
    • CockroachDB.
    • Monzo Bank.
  • 26
    Why is Go used for containers?

    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.

  • 27
    Can constants be computed in Go?

    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.

  • 28
    What is the GOPATH variable in Golang?

    The GOPATH environment variable is employed to symbolized directories out of $GOROOT that combines the source for Go projects including their binaries.

  • 29
    How will you document libraries?

    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/.

  • 30
     Name one Go feature that would be helpful for DevOps.

    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.

  • 31
    What is the difference between C arrays and Go slices?

    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.

  • 32
    List the operators in Golang?

    • Arithmetic operators
    • Bitwise operators
    • Relational operators
    • Logical operators
    • Assignment operators
    • Misc operators
  • 33
    Why is Type assertion used?

    It is used to check values that are held by interface type variable. It is also used to convert various GO types.

  • 34
    How To Delete An Entry From A Map In Go?

    delete() function is used to delete an entry from the map. It requires map and corresponding key which is to be deleted.

  • 35
    What is a select statement in Golang?

    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.

  • 36
    What is the size of int (not int8 or int16)?

    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.

  • 37
    What is rune in GoLang?

    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.

  • 38
    What is the interface? Why do we use it?

    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.

  • 39
     Does Go have exceptions?

    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.

  • 40
    What’s the difference between unbuffered and buffered channels?

    • 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.
  • 41
    Explain GO Interfaces ?

    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

  • 42
    In GO language how you can check variable type at runtime?

    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.

  • 43
    List out the built in support in GO?

    The available built-in-support in GO includes

    • Container: container/list , container/heap
    • Web Server: net/http
    • Cryptography: Crypto/md5 , crypto/sha1
    • Compression: compress/ gzip
    • Database: database/sql
  • 44
    Explain workspace in GO?

    Inside a workspace GO code must be kept. A workspace is a directory hierarchy with three directories at its root.

    • src contains GO source files organized into packages
    • pkg contains package objects and
    • bin contains executable commands
  • 45
    Explain what is string types?

    Public is the default visibility for properties/methods in TypeScript classes.

  • 46
    What are the different methods in Go programming language?

    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.

  • 47
    What is meant by L value and R value in Golang?

    Rvalue

    • Shows on assignment operator’s right side.
    • Always assigned to lvalue.

    Lvalue

    • Shows on assignment operator’s left side.
    • Designated to a variable and not a constant.
  • 48
    What is CGO Golang?

    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.

  • 49
     What is Shadowing?

    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.

  • 50
    What are channels and how can you use them in Golang?

    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.