Skip to content

Complete golang interview guide with questions and answers

Notifications You must be signed in to change notification settings

zillani/go-resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Fundamentals

Table of Contents

  1. What are different data types in go
  2. what is a package in go
  3. what is go workspace
  4. what is GOPATH
  5. what are different directories inside a go project
  6. What are different data structures in go
  7. How to iterate maps in go
  8. How do you create go modules
  9. Explain go modules
  10. Function syntax in go
  11. Method syntax in go
  12. How do you declare inline function
  13. What is the use of empty interface
  14. How access modifiers work in go
  15. what is GOPATH and GOROOT
  16. Explain polymorphism in go
  17. Explain different print formats
  18. What is defautl value of a global, local, & pointer variable
  19. Does go support method overloading, operator overloading, type inheritance
  20. Write a program on pointers
  21. How do you copy slices
  22. How do you copy interfaces and structs
  23. How do you compare two structs maps and slices
  24. How do you compare two interfaces
  25. Explain go get command
  26. How do you do indentation
  27. Explain closures in go
  28. How to increase slice capacity
  29. How to convert string to int
  30. What is & and * in go
  31. Explain ++ and -- in golang
  32. Does go support indexing
  33. How to convert string to byte array & viceversa

gopher

What are different data types in go

  • int8(aka byte)
  • int16
  • int32(aka rune, pronounced as roon (as room))
  • int64
  • float64
  • byte
  • bool
  • string

what is a package in go

a package is a directory where all go files resides.

what is go workspace

a workspace is a directory heirarchy with two root directories,

  • src -> go source files (typically contains multiple vcs based directories example, github.com)
  • bin -> executable files
  • pkg -> shared libs used by executables, example: go mod dependencies

what is GOPATH

It specifies the go workspace

what are different directories inside a go project

  • cmd -> will have sub-directories for cli based
  • pkg -> if you need your code to be re-used by other projects (careful with this)
  • internal -> if you want your code to be private
  • api -> openapi/swagger
  • web -> web components
  • configs -> configurations
  • init -> system init/process manager (systemd, sysv etc..)
  • scripts -> build/installs
  • build -> for deb,rpm,ami,docker images etc..
  • deployments -> docker-compose, k8s yaml, terraform etc.
  • test -> testing

What are different data structures in go

  • Array: fixed length
  • Slice: variable length
  • Maps: key values
  • Struct: is a collection of fields of same/diff types

How to iterate maps in go

using range keyword

  m := map[string]string{ "key1":"val1", "key2":"val2" };
  for k, v := range m {
    fmt.Printf("key[%s] value[%s]\n", k, v)
  }

How do you create go modules

using init command

go module init

Explain go modules

Go module is a dependency management system introduced from go v1.11 a module is a collection of go packages stored in go.mod file

Function syntax in go

func add(x int32, y int32) return int32 {
  return x+y
}

Method syntax in go

type Cal struct {
  x int32
  y int32
}

func (*Cal) add(x int32, y int32) int32 {
  return x + y
}

func main() {
  c := &Cal{}
  c.add(2, 2)
  fmt.Println(c.add(2, 2))
}

How do you declare inline function

inline function is an anonymous function used

Simple

func(){

  fmt.Println("Hello")
}()

Assigning

value := func(){
  fmt.Println("Hello")
}
value()

What is the use of empty interface

Interface has two meanings

  • interface is a set of method declaration
  • emtpy interface is a type used to dynamic type conversion
    • Example, You can pass int or float, its dynamic

      func add (x interface{}, y interface{}) {
        x+y
      }

How access modifiers work in go

there are two types of access modifiers

  • exported (Accessible outside package)
  • unexported (Accessible only within the package)

USING UPPERCASE WILL EXPORT A METHOD OR VARIABLE TO OUTSIDE PACKAGE

  Foo int; // exported - accessible outside package
  bar int; // unexported - only accessible wit

what is GOPATH and GOROOT

GOPATH specifies go workspace and GOROOT specifies go installation directory

Explain polymorphism in go

Polymorphism can be achieved using interface

type User struct {
  username string
  email string
}

type UserI interface {
  getEmail() string
}

func (u *User) getEmail() string {
  return u.username
}

func main(){
  var userI UserI
  userI = &User{"zillani", "shaikzillani@gmail.com"}
  fmt.Println(userI.getEmail())
}

Explain different print formats

Printf -> formats and prints

Sprintf -> formats and doesn't prints, needs to be assigned

%d -> digit

%s -> string

%T -> type

%v -> default format

What is defautl value of a global, local, & pointer variable

  global -> 0

  local -> 0
  
  pointer -> nil

Does go support method overloading, operator overloading, type inheritance

method overloading -> no

operator overloading -> -

type inheritance -> -

Write a program on pointers

Swapping two variables

package main

import "fmt"

func swap(px, py *int){
  temp := *px
  *px = *py
  *py = temp
}

func main(){
  a:=1
  b:=2

  fmt.Println("Before swapping: ",a,b)
  swap(&a,&b)
  fmt.Println("After swapping: ",a,b)
}

How do you create slice

using MAKE or using array declaration

s:= make([]int,3)
s[0] = 1
s[1] = 2
s[2] = 3

s2 := []int{1,2,3}
fmt.Println(s,s2)

How do you copy slices

s1 := []int{1, 2, 3}
s2 := make([]int, 3)
copy(s2, s1)
fmt.Println(s1)
fmt.Println(s2)

How do you copy maps

To copy maps you need to iterate values over the map

  m1 := make(map[int]string)
  m1[1] = "a"
  m1[2] = "b"
  m2 := make(map[int]string)
  for key, value := range m1 {
    m2[key] = value
  }
  fmt.Print(m2)

How do you copy interfaces and structs

copying interface means copy the underlying struct, & structs copied using assignment operator

type Point struct {
        x int
        y int
}

func main() {

        p1 := Point {2,2}
        p2 := p1

        fmt.Println(p1)
        fmt.Println(p2)

}

How do you compare two structs maps and slices

using reflect api,

type Point struct {
  x int
  y int
}

func main() {
  m1 := map[string]int{
    "a": 1,
    "b": 2,
  }
  m2 := map[string]int{
    "a": 1,
    "b": 2,
  }
  s1 := []byte{'a', 'b', 'c'}
  s2 := []byte{'c', 'd', 'e'}

  p1 := Point {2,2}
  p2 := Point {3,3}

  fmt.Println(reflect.DeepEqual(m1, m2))
  fmt.Println(reflect.DeepEqual(s1, s2))
  fmt.Println(reflect.DeepEqual(p1, p2))

How do you compare two interfaces

Using assignment operator

  var z interface{} = 2
  var y interface{} = 2

  fmt.Println(z == y)

Explain go get command

go get is used for installations, suppose if you are installing golint, you do,

go get -u golang.org/x/lint/golint

the source code of golint will be downloaded and copied to $GOPATH/src/golang.org/x/lint and the binary golint will be copied to $GOPATH/bin since this is added to PATH, during installation, the binary will be available to the system

How do you do indentation

using go fmt, example,

go fmt hello.go

go fmt by default uses tabs & spaces are not longer used/recommended.

Explain closures in go

var global func()
func closure() {
 var A int = 1
 func() {
  var B int = 2
  func() {
   var C int = 3
   global = func() {
    fmt.Println(A, B, C)
    fmt.Println(D, E, F) // causes compilation error
   }
   var D int = 4
  }()
  var E int = 5
 }()
 var F int = 6
}
func main() {
 closure()
 global()
}

global function has access to,

  • It has access to A,B,C since the layer of enclosure does not matter
  • It doesn’t have access to D,E,F since their declaration don’t precede
  • Even after execution of “closure” function, it’s local variables were not destroyed. They were accessible in the call to “global” function

How to increase slice capacity

s = s[:cap(s)]

How to convert string to int

import (
  "fmt"
  "strconv"
)
s := "123"
num,_ := strconv.Atoi(s)
fmt.Println(num)

What is & and * in go

The & Operator

& gives address of a variable

The * Operator

\* used to hold the address of a variable

func main(){
        var x int = 2;
        var y *int = &x
        fmt.Println("Address of x ",y)
}

pointer-golang

Explain ++ and -- in golang

++ and -- are statements but not expressions check here

Does go support indexing

check here

How to convert string to byte array & viceversa

string to byte array

b := []byte("ABC€")
fmt.Println(b) // [65 66 67 226 130 172]

byte array to string

s := string([]byte{65, 66, 67, 226, 130, 172})
fmt.Println(s)

About

Complete golang interview guide with questions and answers

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published