21. Write a Go program to check if a string contains a specific substring using the strings package.
Required Input:
A string text = "Golang is amazing!" and a substring sub = "amazing".
Expected Output:
true
Code In Go
package main
import ("fmt")
func main() {
// Check if the string contains a substring
}
Run Code?
Click Run Button to view compiled output
22. Create a Go program to calculate the sum of elements in an integer array.
Required Input:
An array of integers (e.g., [1, 2, 3, 4, 5]).
Expected Output:
15
Code In Go
package main
import "fmt"
func main() {
// Calculate and print the sum of array elements
}
Run Code?
Click Run Button to view compiled output
23. Write a Go program to demonstrate the use of a for loop with range to iterate over an array.
Required Input:
An array of strings (e.g., ["Apple", "Banana", "Cherry"]).
Expected Output:
Index: 0, Value: Apple
Index: 1, Value: Banana
Index: 2, Value: Cherry
Code In Go
package main
import "fmt"
func main() {
// Iterate over the array using a range loop
}
Run Code?
Click Run Button to view compiled output
24. Create a Go program to find the index of an element in a slice using a loop.
Required Input:
A slice of integers (e.g., [10, 20, 30, 40]) and an element to find (e.g., 30).
Expected Output:
2
Code In Go
package main
import "fmt"
func main() {
// Find and print the index of an element in the slice
}
Run Code?
Click Run Button to view compiled output
25. Write a Go program to create and call an anonymous function.
Required Input:
No input required.
Expected Output:
Hello from anonymous function!
Code In Go
package main
import "fmt"
func main() {
// Create and call an anonymous function
}
Run Code?
Click Run Button to view compiled output
26. Create a Go program to demonstrate the use of pointers by modifying a variable's value through a pointer.
Required Input:
A predefined variable x = 10.
Expected Output:
20
Code In Go
package main
import "fmt"
func main() {
// Modify the value of x using a pointer
}
Run Code?
Click Run Button to view compiled output
27. Write a Go program to create a nested struct and access its fields.
Required Input:
A struct with nested fields.
Expected Output:
Name: John
City: New York
State: NY
Code In Go
package main
import "fmt"
type Address struct {
City string
State string
}
type Person struct {
Name string
Address Address
}
func main() {
// Access the fields of a nested struct
}
Run Code?
Click Run Button to view compiled output
28. Create a Go program to read input from the user and print it to the console.
Required Input:
User-provided input.
Expected Output:
Enter something: John Doe
You entered: John Doe
Code In Go
package main
import "fmt"
func main() {
// Read input from the user and print it
}
Run Code?
Click Run Button to view compiled output
29. Write a Go program to implement a simple calculator using functions for addition, subtraction, multiplication, and division.
Required Input:
Two integers and an operation (e.g., 5, 10, "+").
Expected Output:
15
Code In Go
package main
import "fmt"
func add(a, b int) int {
return 0
}
func main() {
// Call the function for the desired operation
}
Run Code?
Click Run Button to view compiled output
30. Create a Go program to demonstrate the use of the recover function for handling panics.
Required Input:
A function that causes a panic.
Expected Output:
Recovered from panic: This is a panic!
Code In Go
package main
import "fmt"
func main() {
// Use recover to handle panic
}
Run Code?
Click Run Button to view compiled output