Skip to content

Instantly share code, notes, and snippets.

@LaurentiuGabriel
Created April 14, 2023 19:09
Show Gist options
  • Save LaurentiuGabriel/f8ce0792167c2238244332356f91ff46 to your computer and use it in GitHub Desktop.
Save LaurentiuGabriel/f8ce0792167c2238244332356f91ff46 to your computer and use it in GitHub Desktop.
GoLang in 40 seconds!
// Golang in 40 seconds
package main
import (
"fmt"
"time"
)
func main() {
// 3. Declare variables
var myVar int = 42
myVar2 := 10
// 4. Functions
result := add(myVar, myVar2)
fmt.Println("Sum:", result)
// 5. Control structures
if result > 50 {
fmt.Println("Greater than 50")
} else {
fmt.Println("Not greater than 50")
}
// 6. Import packages & 7. Goroutines and channels
c := make(chan string)
go printHello(c)
msg := <-c
fmt.Println(msg)
}
// Function example
func add(x int, y int) int {
return x + y
}
// Goroutine example
func printHello(c chan string) {
time.Sleep(1 * time.Second)
c <- "Hello, from goroutine!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment