Frequently asked GOLANG Interview Questions
Vigowebs
Posted on February 10, 2022
What are string literals?
A string literal is a string constant formed by concatenating characters. The two forms of string literal are raw and interpreted string literals. Raw string literals are written within backticks (foo) and are filled with uninterpreted UTF-8 characters. Interpreted string literals are what we commonly think of as strings, written within double quotes and containing any character except newline and unfinished double quotes.
What is the static type declaration of a variable in Go?
Static type variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceeds for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, the compiler needs actual variable declaration at the time of linking of the program.
What are the several built-in supports in Go?
A list of built-in supports in Go:
• Container: container/list, container/heap
• Web Server: net/http
• Cryptography: Crypto/md5, crypto/sha1
• Compression: compress/gzip
• Database: database/sql
What is a goroutine?
A goroutine is a lightweight execution thread in the Go programming language and a function that executes concurrently with the rest of the program. Goroutines are incredibly cheap when compared to traditional threads as the overhead of creating a goroutine is very low. Therefore, they are widely used in Go for concurrent programming. To invoke a function as a goroutine, use the go keyword. Syntax go foo()
We write go before the function foo to invoke it as a goroutine. The function foo()
will run concurrently or asynchronously with the calling function. Example We define a function foo that prints numbers from 1 to 3 along with the passed string. We add a delay using time.Sleep()
inside the for loop. Without the delay, the first goroutine will finish executing even before the second one starts. The delay ensures that the goroutines are running concurrently before the results are shown.
package main
import (
"fmt"
"time"
)
// Prints numbers from 1-3 along with the passed string
func foo(s string) {
for i := 1; i <= 3; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s, ": ", i)
}
}
func main() {
// Starting two goroutines
go foo("1st goroutine")
go foo("2nd goroutine")
// Wait for goroutines to finish before main goroutine ends
time.Sleep(time.Second)
fmt.Println("Main goroutine finished")
}
What are the Go datatypes?
The data type is divided into four categories which are as follows:
- Basic type: Numbers, strings, and Booleans
- Aggregate type: Array and structs
- Reference type: Pointers, slices, maps, functions, and channels
- Interface type
How do you concatenate strings?
The easiest way to concatenate strings is to use the concatenation operator (+
), which allows you to add strings as you would numerical values.
package main
import "fmt"
func main() {
// Creating and initializing strings
// using var keyword
var str1 string
str1 = "Hello "
var str2 string
str2 = "Reader!"
// Concatenating strings
// Using + operator
fmt.Println("New string 1: ", str1+str2)
// Creating and initializing strings
// Using shorthand declaration
str3 := "Welcome"
str4 := "vigowebs"
// Concatenating strings
// Using + operator
result := str3 + " to " + str4
fmt.Println("New string 2: ", result)
}
Does Go have a runtime?
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 (or JavaScript or WebAssembly, for some variant implementations). 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.
How does garbage collector work in Go?
What are the looping constructs in Go?
What are Lvalue and Rvalue in Golang?
What is shadowing in Go?
Explore more GoLang questions with answer, check out our Interview Questions app which contains 1600 questions from 35+ trending topic.
Posted on February 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.