11. Write a Go program to create a slice of strings and append a new string to it.
Required Input:
A slice of strings (e.g., ["Apple", "Banana"]) and a string to append (e.g., "Cherry").
Expected Output:
[Apple Banana Cherry]
Code In Go
package main
import "fmt"
func main() {
// Create a slice and append a new string to it
}
Run Code?
Click Run Button to view compiled output
12. Create a Go program to demonstrate the use of the switch statement by printing the day of the week based on a number (1 for Monday, 2 for Tuesday, etc.).
Required Input:
A predefined variable day = 3.
Expected Output:
Wednesday
Code In Go
package main
import "fmt"
func main() {
// Use a switch statement to print the day of the week
}
Run Code?
Click Run Button to view compiled output
13. Write a Go program to reverse a string word = "Golang".
Required Input:
A predefined string.
Expected Output:
gnaloG
Code In Go
package main
import "fmt"
func reverse(s string) string {
// Write code to reverse the string
return ""
}
func main() {
word := "Golang"
fmt.Println(reverse(word))
}
Run Code?
Click Run Button to view compiled output
14. Create a Go program to declare a map with string keys and integer values, and print a value for a specific key.
Required Input:
A map of string-integer pairs (e.g., {"John": 85, "Alice": 90}).
Expected Output:
90
Code In Go
package main
import "fmt"
func main() {
// Create a map and print the value for a specific key
}
Run Code?
Click Run Button to view compiled output
15. Write a Go program to find the largest number in an array.
Required Input:
An array of integers (e.g., [10, 20, 5, 15]).
Expected Output:
20
Code In Go
package main
import "fmt"
func findLargest(arr []int) int {
// Write code to find the largest number in the array
return 0
}
func main() {
arr := []int{10, 20, 5, 15}
fmt.Println(findLargest(arr))
}
Run Code?
Click Run Button to view compiled output
16. Create a Go program to demonstrate the use of a defer statement.
Required Input:
No input required.
Expected Output:
Function starts
Function ends
Code In Go
package main
import "fmt"
func main() {
// Use a defer statement
}
Run Code?
Click Run Button to view compiled output
17. Write a Go program to calculate the factorial of a number using a loop.
Required Input:
A predefined integer n = 5.
Expected Output:
120
Code In Go
package main
import "fmt"
func factorial(n int) int {
// Write code to calculate the factorial using a loop
return 0
}
func main() {
fmt.Println(factorial(5))
}
Run Code?
Click Run Button to view compiled output
18. Create a Go program to generate a random number between 1 and 100.
Required Input:
No input required.
Expected Output:
99
Code In Go
package main
import ("fmt")
func main() {
// Generate and print a random number
}
Run Code?
Click Run Button to view compiled output
19. Write a Go program to define and use a struct with fields Name and Age.
Required Input:
A struct instance with fields initialized (e.g., {"John", 25}).
Expected Output:
Name: John, Age: 25
Code In Go
package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
// Define a struct and print its fields
}
Run Code?
Click Run Button to view compiled output
20. Create a Go program to define a constant Pi = 3.14159 and print it.
Required Input:
No input required.
Expected Output:
3.14159
Code In Go
package main
import "fmt"
func main() {
// Define and print a constant
}
Run Code?
Click Run Button to view compiled output