In Go, building and deploying efficient binaries, profiling with pprof, and optimizing memory and CPU usage are critical for building high-performance applications. This guide explores these essential topics in Go development.
1. Building and Deploying Go Binaries
What is the command to build a Go binary? a) go build b) go run c) go install d) go compile
How do you build a Go binary for a specific OS and architecture? a) go build -o binary_name b) go build -os=linux -arch=amd64 c) go target -os=linux d) go build –platform=linux-amd64
What command is used to install a Go package? a) go install b) go build c) go get d) go download
Which file extension is used for Go binaries on Linux? a) .go b) .exe c) .bin d) No extension
How do you create a statically linked Go binary? a) go build -static b) go build -ldflags “-extldflags ‘-static'” c) go build -static-link d) go static build
Which command can be used to cross-compile a Go binary for different platforms? a) go crosscompile b) go build -platform c) GOOS=linux GOARCH=amd64 go build d) go build –cross
How do you compile Go code with debugging symbols included? a) go build -debug b) go build -ldflags “-w -s” c) go build -v d) go build -d
Which environment variables are used to target a specific operating system and architecture in Go? a) GOOS and GOARCH b) GO_PLATFORM and GO_ARCH c) TARGET_OS and TARGET_ARCH d) GO_TARGET and GO_ENV
What is the purpose of the go install command? a) It installs the Go package dependencies b) It compiles and installs the Go binary into the workspace c) It builds the package but does not install d) It installs a Go package globally
How can you strip debugging information from a Go binary? a) go build -ldflags “-s -w” b) go build -strip-debug c) go build -o binary -strip d) go strip-debug
2. Profiling with pprof
Which package is used for profiling in Go? a) pprof b) profiling c) go-profile d) benchmark
How do you import the pprof package in a Go program? a) import “net/pprof” b) import “github.com/pprof” c) import “go/pprof” d) import “net/http/pprof”
How do you start profiling CPU usage in a Go program? a) pprof.StartCPUProfile() b) pprof.Start() c) cpuProfile.Start() d) StartProfile(pprof)
What is the default port used by the pprof HTTP server in Go? a) 8000 b) 9090 c) 6060 d) 8080
How do you stop the CPU profiling in Go? a) pprof.StopCPUProfile() b) pprof.EndProfile() c) cpuProfile.Stop() d) StopProfile(pprof)
What command is used to generate a CPU profile using pprof? a) go tool pprof -cpu profile.prof b) go pprof generate cpu_profile c) go tool pprof -text d) go tool pprof -cpu <profile_file>
Which command can be used to display the heap profile? a) pprof display heap b) go tool pprof -heap c) go tool pprof -mem d) go tool pprof -heap_profile
How can you view the heap profile of a Go program? a) go run -memprofile=heap.prof b) go tool pprof heap c) curl http://localhost:6060/debug/pprof/heap d) go profile -heap
Which of the following is the correct method to add pprof to your application? a) Import net/http/pprof b) Import pprof/http c) Import go/pprof d) Import pprof
What is the purpose of the pprof tool in Go? a) It helps monitor memory usage b) It generates binary executables c) It provides performance profiling tools d) It runs unit tests
3. Memory and CPU Optimization Techniques
Which technique helps optimize memory usage in Go? a) Garbage Collection b) Memory pools c) Caching d) All of the above
What is the effect of enabling Go’s garbage collection? a) It automatically frees up unused memory b) It reduces memory usage significantly c) It increases memory usage d) It prevents memory leaks
Which of the following can help reduce memory allocations in Go? a) Using buffered channels b) Using slices and arrays efficiently c) Avoiding unnecessary copies of data d) All of the above
What is the role of the sync.Pool type in Go? a) It is used to optimize memory usage by reusing objects b) It handles garbage collection automatically c) It helps in concurrent memory management d) It reduces the CPU usage
Which of the following is a common CPU optimization technique in Go? a) Parallel processing b) Using memory-mapped files c) Implementing caching algorithms d) All of the above
How does the Go runtime optimize CPU usage during concurrent execution? a) By using goroutines b) By using system threads c) By using CPU affinity d) By distributing workloads across multiple cores
What command can be used to view the CPU usage profile in Go? a) go tool pprof -cpu b) go tool pprof cpu.profile c) go cpu-profile d) pprof -cpu-profile
What is a good strategy for reducing memory usage in Go? a) Minimizing the number of allocations b) Reducing the number of goroutines c) Keeping data in a memory-mapped file d) Both a and b
Which is a best practice for managing memory in Go? a) Use value types whenever possible b) Use pointers for large objects c) Avoid creating large slices in loops d) All of the above
How can you profile CPU performance in Go? a) Using go tool pprof b) Using pprof.CPUProfile() c) Using the go profiler d) Using cpu.profiler
Answers Table
QNo
Answer (Option with Text)
1
a) go build
2
b) go build -os=linux -arch=amd64
3
c) go get
4
d) No extension
5
b) go build -ldflags “-extldflags ‘-static'”
6
c) GOOS=linux GOARCH=amd64 go build
7
b) go build -ldflags “-w -s”
8
a) GOOS and GOARCH
9
b) It compiles and installs the Go binary into the workspace