Just some question that seem to pop up often
1. Golang benefits
- optimized for concurrency that scales
- considered very readable
- automatic garbage collection
2. What are string literals?
- A string literal is a constant formed by concatenating characters
- Raw string literals with backsticks and filled with uninterpreted UTF-8 characters. Strings use double quotes
3. Examples of datatypes
- Method, Boolean, Numeric, String, Array, Slice, Struct, Pointer, Function, Interface, Map, Channel.
4. What are packages in a Go program?
Packages (pkg) are directories within your Go workspace that contains files. "fmt"
5. What form of type conversion does Go support
Go supports explicit type conversion to satisfy its strict typing requirements.
i := 50
j := 42.5 // float64
sum := i + int(j) // j is converted to int
6. What is a goroutine? How do you stop in?
A goroutine is a function or method that executes concurrently alongside any other goroutines using a special gorutine thread.
To create a go routine, add the go keyword before the function declaration.
go f(x, y, z)
You can stop the gorutine by sending it a signal channel
package main
func main() {
quit := make(chan bool)
go func() {
for {
select {
case <-quit:
return
default:
// ...
}
}
}
}()
// ...
quit <- true
}
7. How do you check a variable type at runtime?
The type Switch is the best way to check a variable's type at runtime. The Type Swith evalutes variables by type rather than value
package main
import "fmt"
func do(i interface{} {
switch v := i.(type) {
case int:
fmt.Printf("Double %v is %v\n", v, v*2)
case string:
fmt.Printf("%q is %v bytes long\n", v, len(v)
default:
fmt.Printf("I dont' know type % T\n", v)
}
}
func main() {
do(21)
do("hello")
do(true)
}
8. How do you concatenate strings?
To concatenate strings just use (+) operator
9. Explain the steps of testing with Golang
It uses automated testing of packages with custom testing suites.
It usually starts with test.go and includes a TestXxx function, where Xxx is replaced with the name of the feature you're testing.
10. What are function closures?
Function closures are a function value that references variables from outside its body.
package main
import "fmt"
func addr() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, nes := addr(), addr()
for i := 0; i < 10; i++ {
fmt.Println(pos(i), neg(-2*i))
}
}
11. How do we perform inheritance with golang?
This is a bit of a trick question: there is no inheritance in Golang because it does not support classes
You can mimic the behavior with a composition to use an existing struct object to define a starting behavior of a new object. Once the new object is created, functionality can be extended beyond the original struct.
type Animal struct {
// ...
}
func (a *Animal) Eat() { ...}
func (a *Animal) Sleep() { ... }
func (a *Animal) Run() { ... }
type Dog struct {
Animal
// ...
}
The Animal struct contains Eat(), Sleep(), and Run() functions. These functions are embedded into the child struct Dog by simply listing the struct at the top of the implementation of Dog.
12 Explain Go interfaces. What are they and how do they work?
https://www.educative.io/blog/50-golang-interview-questions
To post a comment you need to login first.