21. Write a Go program to calculate the SHA256 hash of a given string.
Required Input:
A string (e.g., "Hello, World!").
Expected Output:
SHA256 Hash: dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
Code In Go
package main
import "fmt"
func main() {
// Calculate and print the SHA256 hash of a string
}
Run Code?
Click Run Button to view compiled output
22. Create a Go program to demonstrate the use of reflection to access struct fields dynamically.
Required Input:
A struct Person{Name: "Alice", Age: 25}.
Expected Output:
Field: Name, Value: Alice
Field: Age, Value: 25
Code In Go
package main
import "fmt"
func main() {
// Use reflection to access struct fields dynamically
}
Run Code?
Click Run Button to view compiled output
23. Write a Go program to demonstrate the use of a mutex to avoid race conditions.
Required Input:
A shared counter incremented by multiple goroutines.
Expected Output:
Final Counter: 10
Code In Go
package main
import "fmt"
func main() {
// Use a mutex to synchronize access to a shared variable
}
Run Code?
Click Run Button to view compiled output
24. Create a Go program to implement a producer-consumer problem using channels.
Required Input:
A producer sends tasks (e.g., Task 1, Task 2), and a consumer processes them.
Expected Output:
Produced: Task 1
Consumed: Task 1
Consumed: Task 2
Produced: Task 2
Produced: Task 3
Consumed: Task 3
Code In Go
package main
import "fmt"
func producer(ch chan string) {
// Send tasks to the channel
}
func consumer(ch chan string) {
// Process tasks from the channel
}
func main() {
// Set up producer and consumer
}
Run Code?
Click Run Button to view compiled output
25. Write a Go program to calculate the factorial of numbers in parallel using goroutines.
Required Input:
An array of numbers (e.g., [5, 6, 7]).
Expected Output:
5040
120
720
Code In Go
package main
import "fmt"
func calculateFactorial(n int, result chan int) {
// Calculate factorial and send the result to the channel
}
func main() {
// Launch goroutines to calculate factorials in parallel
}
Run Code?
Click Run Button to view compiled output
26. Create a Go program to compress and decompress a string using the compress/gzip package.
Required Input:
A string (e.g., "Hello, World!").
Expected Output:
Compressed Data: [31 139 8 0 0 0 0 0 0 255 242 72 205 201 201 215 81 8 207 47 202 73 81 4 4 0 0 255 255 208 195 74 236 13 0 0 0]
Decompressed Data: Hello, World!
Code In Go
package main
import "fmt"
func main() {
// Compress and decompress a string using gzip
}
Run Code?
Click Run Button to view compiled output