Skip to content

Add comprehensive Go interview preparation programs #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 01_hello_world.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 01_hello_world.go - Basic Go program structure
package main

import "fmt"

func main() {
fmt.Println("Hello, Go!")
fmt.Println("This is a basic Go program demonstrating:")
fmt.Println("- Package declaration")
fmt.Println("- Import statement")
fmt.Println("- Main function")
}
55 changes: 55 additions & 0 deletions 02_data_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 02_data_types.go - Demonstrates all basic data types in Go
package main

import (
"fmt"
)

func main() {
fmt.Println("=== GO DATA TYPES DEMONSTRATION ===")

// Numeric Types
var myInt int = 42
var myInt8 int8 = 127
var myInt16 int16 = 32767
var myInt32 int32 = 2147483647
var myInt64 int64 = 9223372036854775807

var myUint uint = 42
var myUint8 uint8 = 255
var myByte byte = 255 // byte is alias for uint8

var myFloat32 float32 = 3.14
var myFloat64 float64 = 3.141592653589793

var myComplex64 complex64 = 1 + 2i
var myComplex128 complex128 = 1 + 2i

// String and Rune
var myString string = "Hello, Go!"
var myRune rune = 'A' // rune is alias for int32

// Boolean
var myBool bool = true

// Print all types
fmt.Printf("Int: %d (type: %T)\n", myInt, myInt)
fmt.Printf("Int8: %d (type: %T)\n", myInt8, myInt8)
fmt.Printf("Int16: %d (type: %T)\n", myInt16, myInt16)
fmt.Printf("Int32: %d (type: %T)\n", myInt32, myInt32)
fmt.Printf("Int64: %d (type: %T)\n", myInt64, myInt64)

fmt.Printf("Uint: %d (type: %T)\n", myUint, myUint)
fmt.Printf("Uint8: %d (type: %T)\n", myUint8, myUint8)
fmt.Printf("Byte: %d (type: %T)\n", myByte, myByte)

fmt.Printf("Float32: %.2f (type: %T)\n", myFloat32, myFloat32)
fmt.Printf("Float64: %.15f (type: %T)\n", myFloat64, myFloat64)

fmt.Printf("Complex64: %g (type: %T)\n", myComplex64, myComplex64)
fmt.Printf("Complex128: %g (type: %T)\n", myComplex128, myComplex128)

fmt.Printf("String: %s (type: %T)\n", myString, myString)
fmt.Printf("Rune: %c (type: %T)\n", myRune, myRune)
fmt.Printf("Boolean: %t (type: %T)\n", myBool, myBool)
}
51 changes: 51 additions & 0 deletions 03_zero_values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 03_zero_values.go - Demonstrates zero values in Go
package main

import "fmt"

type Person struct {
Name string
Age int
}

func main() {
fmt.Println("=== GO ZERO VALUES DEMONSTRATION ===")

// Basic types zero values
var (
b bool
i int
f float64
c complex128
s string
r rune
bt byte
)

fmt.Printf("bool zero value: %t\n", b)
fmt.Printf("int zero value: %d\n", i)
fmt.Printf("float64 zero value: %f\n", f)
fmt.Printf("complex128 zero value: %g\n", c)
fmt.Printf("string zero value: '%s' (empty string)\n", s)
fmt.Printf("rune zero value: %d\n", r)
fmt.Printf("byte zero value: %d\n", bt)

// Composite types zero values
var (
slice []int
dmap map[string]int
ptr *int
iface interface{}
ch chan int
fn func()
person Person
)

fmt.Printf("\nslice zero value: %v (nil: %t)\n", slice, slice == nil)
fmt.Printf("map zero value: %v (nil: %t)\n", dmap, dmap == nil)
fmt.Printf("pointer zero value: %v (nil: %t)\n", ptr, ptr == nil)
fmt.Printf("interface zero value: %v (nil: %t)\n", iface, iface == nil)
fmt.Printf("channel zero value: %v (nil: %t)\n", ch, ch == nil)
fmt.Printf("function zero value: %v (nil: %t)\n", fn, fn == nil)
fmt.Printf("struct zero value: %+v\n", person)
}
67 changes: 67 additions & 0 deletions 04_arrays_slices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 04_arrays_slices.go - Demonstrates arrays vs slices
package main

import "fmt"

func main() {
fmt.Println("=== ARRAYS VS SLICES DEMONSTRATION ===")

// Arrays - Fixed size
fmt.Println("--- ARRAYS ---")
var arr1 [5]int // Declaration with zero values
arr2 := [5]int{1, 2, 3, 4, 5} // Declaration with initialization
arr3 := [...]int{10, 20, 30} // Let compiler count the elements

fmt.Printf("arr1: %v (length: %d)\n", arr1, len(arr1))
fmt.Printf("arr2: %v (length: %d)\n", arr2, len(arr2))
fmt.Printf("arr3: %v (length: %d)\n", arr3, len(arr3))

// Modifying array elements
arr1[0] = 100
fmt.Printf("arr1 after modification: %v\n", arr1)

// Slices - Dynamic size
fmt.Println("\n--- SLICES ---")
var slice1 []int // nil slice
slice2 := []int{1, 2, 3, 4, 5} // slice literal
slice3 := make([]int, 5) // make function with length
slice4 := make([]int, 3, 10) // make with length and capacity

fmt.Printf("slice1: %v (len: %d, cap: %d, nil: %t)\n", slice1, len(slice1), cap(slice1), slice1 == nil)
fmt.Printf("slice2: %v (len: %d, cap: %d)\n", slice2, len(slice2), cap(slice2))
fmt.Printf("slice3: %v (len: %d, cap: %d)\n", slice3, len(slice3), cap(slice3))
fmt.Printf("slice4: %v (len: %d, cap: %d)\n", slice4, len(slice4), cap(slice4))

// Slicing operations
fmt.Println("\n--- SLICING OPERATIONS ---")
numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Printf("Original: %v\n", numbers)
fmt.Printf("numbers[2:5]: %v\n", numbers[2:5])
fmt.Printf("numbers[:4]: %v\n", numbers[:4])
fmt.Printf("numbers[6:]: %v\n", numbers[6:])
fmt.Printf("numbers[:]: %v\n", numbers[:])

// Append operations
fmt.Println("\n--- APPEND OPERATIONS ---")
var dynamicSlice []int
fmt.Printf("Initial: %v (len: %d, cap: %d)\n", dynamicSlice, len(dynamicSlice), cap(dynamicSlice))

dynamicSlice = append(dynamicSlice, 1)
fmt.Printf("After append(1): %v (len: %d, cap: %d)\n", dynamicSlice, len(dynamicSlice), cap(dynamicSlice))

dynamicSlice = append(dynamicSlice, 2, 3, 4)
fmt.Printf("After append(2,3,4): %v (len: %d, cap: %d)\n", dynamicSlice, len(dynamicSlice), cap(dynamicSlice))

moreNumbers := []int{5, 6, 7}
dynamicSlice = append(dynamicSlice, moreNumbers...)
fmt.Printf("After append(slice...): %v (len: %d, cap: %d)\n", dynamicSlice, len(dynamicSlice), cap(dynamicSlice))

// Copy operations
fmt.Println("\n--- COPY OPERATIONS ---")
src := []int{1, 2, 3, 4, 5}
dst := make([]int, len(src))
n := copy(dst, src)
fmt.Printf("Source: %v\n", src)
fmt.Printf("Destination: %v\n", dst)
fmt.Printf("Elements copied: %d\n", n)
}
95 changes: 95 additions & 0 deletions 05_type_conversions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 05_type_conversions.go - Demonstrates type conversions and assertions
package main

import (
"fmt"
"strconv"
)

func main() {
fmt.Println("=== TYPE CONVERSIONS DEMONSTRATION ===")

// Numeric conversions
fmt.Println("--- NUMERIC CONVERSIONS ---")
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

fmt.Printf("int: %d -> float64: %f -> uint: %d\n", i, f, u)

// String conversions
fmt.Println("\n--- STRING CONVERSIONS ---")

// Number to string
num := 123
str1 := strconv.Itoa(num)
str2 := fmt.Sprintf("%d", num)

fmt.Printf("Number %d to string: '%s' (using Itoa)\n", num, str1)
fmt.Printf("Number %d to string: '%s' (using Sprintf)\n", num, str2)

// String to number
str := "456"
num2, err := strconv.Atoi(str)
if err != nil {
fmt.Printf("Error converting string to int: %v\n", err)
} else {
fmt.Printf("String '%s' to number: %d\n", str, num2)
}

// Float conversions
floatStr := "3.14159"
floatNum, err := strconv.ParseFloat(floatStr, 64)
if err != nil {
fmt.Printf("Error converting string to float: %v\n", err)
} else {
fmt.Printf("String '%s' to float: %f\n", floatStr, floatNum)
}

// Boolean conversions
boolStr := "true"
boolVal, err := strconv.ParseBool(boolStr)
if err != nil {
fmt.Printf("Error converting string to bool: %v\n", err)
} else {
fmt.Printf("String '%s' to bool: %t\n", boolStr, boolVal)
}

// Type assertions with interfaces
fmt.Println("\n--- TYPE ASSERTIONS ---")
var data interface{} = "Hello, Go!"

// Type assertion with ok pattern
if str, ok := data.(string); ok {
fmt.Printf("data is a string: '%s'\n", str)
} else {
fmt.Println("data is not a string")
}

// Type assertion without ok pattern (can panic)
str3 := data.(string)
fmt.Printf("Direct assertion: '%s'\n", str3)

// Type switch
fmt.Println("\n--- TYPE SWITCH ---")
testTypeSwitch(42)
testTypeSwitch("Hello")
testTypeSwitch(3.14)
testTypeSwitch(true)
testTypeSwitch([]int{1, 2, 3})
}

func testTypeSwitch(data interface{}) {
switch v := data.(type) {
case int:
fmt.Printf("Integer: %d\n", v)
case string:
fmt.Printf("String: '%s'\n", v)
case float64:
fmt.Printf("Float64: %f\n", v)
case bool:
fmt.Printf("Boolean: %t\n", v)
default:
fmt.Printf("Unknown type: %T with value: %v\n", v, v)
}
}
Loading