[go: up one dir, main page]

0% found this document useful (0 votes)
6 views10 pages

Golang Programs Accenture

Uploaded by

krishnayt9080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Golang Programs Accenture

Uploaded by

krishnayt9080
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Reverse a Slice In-Place

package main import ( "fmt" ) // reverseInPlace reverses the input slice of


integers in-place func reverseInPlace(nums []int) []int { left, right := 0,
len(nums)-1 for left < right { nums[left], nums[right] = nums[right], nums[left]
left++ right-- } return nums } func main() { a := []int{1, 2, 3, 4}
fmt.Println("Original:", a) reversed := reverseInPlace(a)
fmt.Println("Reversed:", reversed) }
2. Check for Palindrome

package main import ( "fmt" "strings" ) func isPalindrome(s string) bool { s =


strings.ToLower(s) for i := 0; i < len(s)/2; i++ { if s[i] != s[len(s)-1-i] {
return false } } return true } func main() { fmt.Println(isPalindrome("Madam"))
fmt.Println(isPalindrome("GoLang")) }
3. Print Numbers Concurrently (Goroutine)

package main import ( "fmt" "time" ) func printNumbers() { for i := 1; i <= 5; i++
{ fmt.Println("Number:", i) time.Sleep(200 * time.Millisecond) } } func main() {
go printNumbers() go printNumbers() time.Sleep(2 * time.Second) }
4. Channel Communication

package main import "fmt" func sendMessage(ch chan string) { ch <- "Hello from
Goroutine" } func main() { ch := make(chan string) go sendMessage(ch) msg := <-ch
fmt.Println(msg) }
5. Character Frequency in String

package main import "fmt" func charFrequency(s string) map[rune]int { freq :=


make(map[rune]int) for _, ch := range s { freq[ch]++ } return freq } func main() {
s := "accenture" result := charFrequency(s) fmt.Println(result) }
6. Fibonacci Using Recursion

package main import "fmt" func fibonacci(n int) int { if n <= 1 { return n }
return fibonacci(n-1) + fibonacci(n-2) } func main() { for i := 0; i < 10; i++ {
fmt.Print(fibonacci(i), " ") } }
7. Interface with Circle and Rectangle

package main import ( "fmt" "math" ) type Shape interface { Area() float64 } type
Circle struct { Radius float64 } func (c Circle) Area() float64 { return math.Pi *
c.Radius * c.Radius } type Rectangle struct { Width, Height float64 } func (r
Rectangle) Area() float64 { return r.Width * r.Height } func main() { var s Shape
s = Circle{Radius: 5} fmt.Println("Circle Area:", s.Area()) s = Rectangle{Width:
4, Height: 5} fmt.Println("Rectangle Area:", s.Area()) }
8. Panic and Recover Example

package main import "fmt" func safeDivide(a, b int) { defer func() { if r :=


recover(); r != nil { fmt.Println("Recovered from panic:", r) } }()
fmt.Println("Result:", a/b) } func main() { safeDivide(10, 2) safeDivide(5, 0) }
9. Defer for File Handling

package main import ( "fmt" "os" ) func main() { file, err :=


os.Create("example.txt") if err != nil { fmt.Println("Error:", err) return }
defer file.Close() file.WriteString("Hello Accenture!") }
10. Sort Map by Keys

package main import ( "fmt" "sort" ) func main() { m := map[string]int{"b": 2,


"c": 3, "a": 1} keys := make([]string, 0, len(m)) for k := range m { keys =
append(keys, k) } sort.Strings(keys) for _, k := range keys { fmt.Println(k,
"=>", m[k]) } }

You might also like