Open mtchuyen opened 3 years ago
https://betterprogramming.pub/how-to-crack-the-top-25-golang-interview-questions-a94396d6c808
Golang Technical Interview @TCMLabs Source: https://blog.tcmlabs.fr/golang-technical-interview-tcmlabs-a70c02639381
Here is a sample of those questions, sorted by level of experience.
What is a function?
What is a method?
What is a Structure?
What is a pointer?
What is “interface{}”?
What is the package fmt? What are the differences between:
fmt.Println()
---
fmt.Printf()
---
fmt.Sprintf()
How are the error managed in Go?
func (o obj) Call() err
---
func (o *obj) Call() err
type Blob struct {
Name string `json:"name"`
Age string `json:"order"`
}
func main() {
var blob []Blob
var jsonBlob = []byte(`[
{"name": "Adam", "age": "10"},
{"name": "Eve", "age": "100"}
]`)
json.Unmarshal(jsonBlob, &blob)
}
--------------------------------------------------------------------
func main() {
var blob []map[string]interface{}
var jsonBlob = []byte(`[
{"name": "Adam", "age": "10"},
{"name": "Eve", "age": "100"}
]`)
json.Unmarshal(jsonBlob, &blob)
}
Explain what this is doing? Compare it to the code from the previous question?
func main() {
var blob []map[string]interface{}
var jsonBlob = []byte(`[
{"name": "Adam", "age": "10"},
{"name": "Eve", "age": "100"}
]`)
json.NewDecoder(bytes.NewReader(jsonBlob)).Decode(&blob)
}
ch := make(chan int)
ch <- 7
res := <- ch
fmt.Println(res)
func (f [HandlerFunc](https://pkg.go.dev/net/http#HandlerFunc)) ServeHTTP(w [ResponseWriter](https://pkg.go.dev/net/http#ResponseWriter), r *[Request](https://pkg.go.dev/net/http#Request))
type HandlerFunc func([ResponseWriter](https://pkg.go.dev/net/http#ResponseWriter), *[Request](https://pkg.go.dev/net/http#Request))
type Handler interface {
ServeHTTP([ResponseWriter](https://pkg.go.dev/net/http#ResponseWriter), *[Request](https://pkg.go.dev/net/http#Request))
}
func ListenAndServe(addr [string](https://pkg.go.dev/builtin#string), handler [Handler](https://pkg.go.dev/net/http#Handler)) [error](https://pkg.go.dev/builtin#error)
package main
import ( "fmt" "log" "net/http" )
func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) }
func main() { http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":8080", nil)) }
- Explain this code and compare it to the previous question:
type Layer0 struct { }
func (l Layer0) ServeHTTP(w http.ResponseWriter, r http.Request) { if r.URL.Path[1:] == "test" { fmt.Fprintf(w, "Test page") } http.Error(w, "Page Not Found", http.StatusNotFound) }
func main() { myServer := &Layer0{} http.ListenAndServe(":8080", myServer) }
- What are the advantages and drawbacks of using a go router?
Source: https://dsysd-dev.medium.com/20-intermediate-level-golang-interview-questions-da11917acb51
Goroutines enable concurrent execution in Golang, allowing functions to run concurrently without blocking each other.
Golang provides synchronization primitives like mutexes and channels to safely access and modify shared resources in concurrent scenarios.
A defer statement schedules a function call to be executed later, while a panic is a runtime error that triggers immediate program termination.
Golang uses the error type to handle and propagate errors. Functions often return an error as the last return value, which can be checked for nil to identify errors.
The context package provides a mechanism for managing Goroutines and handling cancellation or timeouts in a controlled manner.
Golang has a built-in testing package called testing. You can write test functions with names starting with Test and use the go test command to run the tests.
Pointers in Golang hold the memory addresses of values. They are used to indirectly access and modify values, allowing efficient memory management and in-place modifications.
Shallow copy creates a new copy of the structure but references the same underlying data, while deep copy creates a new copy with new, separate data.
Interfaces define a set of method signatures. Types that implement these methods implicitly satisfy the interface, allowing polymorphism and abstraction.
Golang provides the encoding/json package to encode Go types into JSON and decode JSON into Go types using the json.Marshal and json.Unmarshal functions.
The sync.WaitGroup is used to wait for a collection of Goroutines to finish executing before proceeding.
The sync.Pool provides a pool of reusable objects, allowing efficient memory reuse and reducing the overhead of object allocation.
Command-line arguments can be accessed using the os.Args variable, which provides a slice of strings representing the command-line arguments.
Middleware in Golang is used to intercept and process HTTP requests and responses, allowing common functionalities like authentication, logging, or rate limiting to be shared across multiple routes.
Golang provides the os package for file operations. You can use functions like os.Open, os.Create, and os.ReadFile to work with files.
Reflection in Golang enables the inspection of types, values, and structs at runtime. It allows dynamic type checks and accessing and manipulating data without knowing the type at compile-time.
Golang encourages returning errors explicitly as a return value and provides functions like errors.New and fmt.Errorf for creating and formatting errors.
The go.mod file is used to define and manage the dependencies of a Golang project. It allows versioning and tracking of external packages used in the project.
Golang provides various database drivers and packages, such as database/sql, to interact with databases. These packages offer functions for connecting, querying, and modifying database data.
Method receivers in Golang are special types of functions associated with a struct or a type. They allow performing actions or computations on the values of that type.
When in doubt, use a pointer receiver https://groups.google.com/g/golang-nuts/c/Qb4IAEbpziU
There are six (6) permutations of an interface and the variable of the interface type, two (2) of which will not compile:
Permutation #4 and #6 are consistent with the description above, and they both have a value variable in common.
50-golang-interview-questions
https://www.educative.io/blog/50-golang-interview-questions