Get ready for your next interview with our comprehensive question library
Go (or Golang) is an open-source programming language developed by Google. Its main characteristics include:
Go provides multiple ways to declare variables:
// Explicit type declaration
var name string = "John"
var age int = 30
// Type inference
var name = "John"
var age = 30
// Short variable declaration (inside functions only)
name := "John"
age := 30
// Multiple variable declaration
var a, b, c int = 1, 2, 3
x, y := 10, "hello"
var
can be used at package and function level, supports explicit type declaration, and initializes with zero values if not assigned:=
(short variable declaration) can only be used inside functions, always requires initialization, and uses type inference// Package level - must use var
var globalVar string
func main() {
// Both are valid inside functions
var localVar string // Zero value: ""
shortVar := "hello" // Type inferred as string
}
Go's basic data types include:
Numeric types:
int
, int8
, int16
, int32
, int64
uint
, uint8
, uint16
, uint32
, uint64
float32
, float64
complex64
, complex128
Other basic types:
bool
(true/false)string
(UTF-8 encoded)byte
(alias for uint8)rune
(alias for int32, represents Unicode code point)Constants are declared using the const
keyword and cannot be changed after declaration:
const Pi = 3.14159
const MaxUsers = 100
// Grouped constants
const (
StatusOK = 200
StatusError = 500
StatusPending = 202
)
// iota for auto-incrementing constants
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
)
Arrays have fixed size, while slices are dynamic:
// Array - fixed size
var arr [5]int = [5]int{1, 2, 3, 4, 5}
// Slice - dynamic size
var slice []int = []int{1, 2, 3, 4, 5}
slice = append(slice, 6) // Can grow
// Creating slices
slice1 := make([]int, 5) // length 5, capacity 5
slice2 := make([]int, 5, 10) // length 5, capacity 10
Slices are more commonly used because of their flexibility and built-in functions like append()
.
Maps are key-value pairs similar to hash tables:
// Creating maps
ages := make(map[string]int)
ages["Alice"] = 30
ages["Bob"] = 25
// Map literal
ages := map[string]int{
"Alice": 30,
"Bob": 25,
}
// Checking if key exists
age, exists := ages["Alice"]
if exists {
fmt.Println("Alice's age:", age)
}
// Deleting keys
delete(ages, "Bob")
Structs are custom types that group related data:
type Person struct {
Name string
Age int
City string
}
// Creating struct instances
p1 := Person{Name: "Alice", Age: 30, City: "NYC"}
p2 := Person{"Bob", 25, "LA"} // positional
p3 := &Person{Name: "Charlie", Age: 35} // pointer to struct
// Accessing fields
fmt.Println(p1.Name)
p1.Age = 31
Pointers store memory addresses of variables:
var x int = 42
var p *int = &x // p points to x
fmt.Println(*p) // Dereference: prints 42
*p = 21 // Change value through pointer
fmt.Println(x) // prints 21
// Zero value of pointer is nil
var ptr *int
if ptr == nil {
fmt.Println("Pointer is nil")
}
Functions are defined using the func
keyword:
// Basic function
func add(a, b int) int {
return a + b
}
// Multiple return values
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
// Named return values
func calculate(a, b int) (sum, product int) {
sum = a + b
product = a * b
return // naked return
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime