Golang was first published in 2009 with a combined effort of a lot of contributors and a dedicated team at Google. Go is used for creating big data systems, cloud-native applications, distributed systems, blockchain software, etc. Here are some of the most asked python questions you will probably get asked on your next interview in 2021.
You can also find all 68 answers here ππΌ https://devinterview.io/dev/golang-interview-questions
Go isn't a library and not a framework, it's a new language.
Go is mostly in the C family (basic syntax), with significant input from the Pascal/Modula/Oberon family (declarations, packages). Go does have an extensive library, called the runtime, that is part of every Go program. 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.
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.
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.
Go is an attempt to introduce a new, concurrent, garbage-collected language with fast compilation and the following benefits:
- It is possible to compile a large Go program in a few seconds on a single computer.
- Go provides a model for software construction that makes dependency analysis easy and avoids much of the overhead of C-style include files and libraries.
- Go's type system has no hierarchy, so no time is spent defining the relationships between types. Also, although Go has static types, the language attempts to make types feel lighter weight than in typical OO languages.
- Go is fully garbage-collected and provides fundamental support for concurrent execution and communication.
- By its design, Go proposes an approach for the construction of system software on multicore machines.
Go is very strict about explicit typing. There is no automatic type promotion or conversion. Explicit type conversion is required to assign a variable of one type to another.
Consider:
i := 55 //int
j := 67.8 //float64
sum := i + int(j) //j is converted to int
Yes. Variables of different types can be declared in one go using type inference.
var a, b, c = 3, 4, "foo"
Go was born out of frustration with existing languages and environments for systems programming.
Go is an attempt to have:
- an interpreted, dynamically typed language with
- the efficiency and safety of a statically typed, compiled language
- support for networked and multicore computing
- be fast in compilation
To meet these goals required addressing a number of linguistic issues: an expressive but lightweight type system; concurrency and garbage collection; rigid dependency specification; and so on. These cannot be addressed well by libraries or tools so a new language was born.
Goroutines are functions or methods 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. Its common for Go applications to have thousands of Goroutines running concurrently.
No, Go takes a different approach. For plain error handling, Go's multi-value returns make it easy to report an error without overloading the return value. Go code uses error values to indicate an abnormal state.
Consider:
func Open(name string) (file *File, err error)
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
// do something with the open *File f
Following are the benefits of using Go programming:
- Support for environment adopting patterns similar to dynamic languages. For example type inference (
x := 0
is valid declaration of a variablex
of typeint
). - Compilation time is fast.
- In built concurrency support: light-weight processes (via goroutines), channels, select statement.
- Conciseness, Simplicity, and Safety.
- Support for Interfaces and Type embedding.
- The go compiler supports static linking. All the go code can be statically linked into one big fat binary and it can be deployed in cloud servers easily without worrying about dependencies.
A pointer variable can hold the address of a variable.
Consider:
var x = 5 var p *int p = &x
fmt.Printf("x = %d", *p)
Here x
can be accessed by *p
.
A Go function can return multiple values.
Consider:
package main import "fmt"
func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("Mahesh", "Kumar") fmt.Println(a, b) }
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.
In Go, a string
is a primitive type, which means it is read-only, and every manipulation of it will create a new string.
So if I want to concatenate strings many times without knowing the length of the resulting string, what's the best way to do it?
Beginning with Go 1.10 there is a strings.Builder
. A Builder is used to efficiently build a string using Write methods. It minimizes memory copying. The zero value is ready to use.
package main
import ( "strings" "fmt" )
func main() { var str strings.Builder
<span class="token cVar">for</span> i <span class="token cBase">:=</span> <span class="token cNum">0</span><span class="token cBase">;</span> i <span class="token cBase"><</span> <span class="token cNum">1000</span><span class="token cBase">;</span> i<span class="token cBase">++</span> <span class="token cBase">{</span> str<span class="token cBase">.</span><span class="token cMod">WriteString</span><span class="token cBase">(</span><span class="token cString">"a"</span><span class="token cBase">)</span> <span class="token cBase">}</span> fmt<span class="token cBase">.</span><span class="token cMod">Println</span><span class="token cBase">(</span>str<span class="token cBase">.</span><span class="token cMod">String</span><span class="token cBase">(</span><span class="token cBase">)</span><span class="token cBase">)</span>
}
In Go there are various ways to return a struct value or slice thereof. Could you explain the difference?
type MyStruct struct { Val int }
func myfunc() MyStruct { return MyStruct{Val: 1} }
func myfunc() *MyStruct { return &MyStruct{} }
func myfunc(s *MyStruct) { s.Val = 1 }
Shortly:
- the first returns a copy of the struct,
- the second a pointer to the struct value created within the function,
- the third expects an existing struct to be passed in and overrides the value.
Variable is the name given to a memory location to store a value of a specific type. There are various syntaxes to declare variables in go.
// 1 - variable declaration, then assignment var age int age = 29
// 2 - variable declaration with initial value var age2 int = 29
// 3 - Type inference var age3 = 29
// 4 - declaring multiple variables var width, height int = 100, 50
// 5 - declare variables belonging to different types in a single statement var (
name1 = initialvalue1, name2 = initialvalue2 ) // 6 - short hand declaration name, age4 := "naveen", 29 //short hand declaration
If a variable is not assigned any value, go automatically initialises it with the zero value of the variable's type. Go is strongly typed, so variables declared as belonging to one type cannot be assigned a value of another type.