Last active
August 8, 2024 17:24
-
-
Save jboursiquot/20cea495d6620d2a286e3ca79ffc0523 to your computer and use it in GitHub Desktop.
TNS Go samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Variable Declaration: Go supports both explicit and implicit variable declaration. | |
// Explicit declaration requires specifying the type, while implicit declaration uses type inference. | |
// Example of explicit declaration: | |
var message string = "Hello, Go!" // explict type specified | |
// Example of implicit declaration: | |
count := 42 | |
// Functions: Functions in Go are defined using the func keyword. | |
// They can return multiple values, a feature that simplifies error handling. | |
// Example of a simple function: | |
func add(a int, b int) int { | |
return a + b | |
} | |
// Example of a function returning multiple values: | |
func divide(a, b int) (int, error) { | |
if b == 0 { | |
return 0, fmt.Errorf("division by zero") | |
} | |
return a / b, nil | |
} | |
// Control Structures: Go includes standard control structures like if-else, for loops, and switch statements. | |
// Example of if-else statement: | |
if count > 10 { | |
fmt.Println("Count is greater than 10") | |
} else { | |
fmt.Println("Count is 10 or less") | |
} | |
// Example of for loop: | |
for i := 0; i < 5; i++ { | |
fmt.Println(i) | |
} | |
// Example of switch statement: | |
switch day := time.Now().Weekday(); day { | |
case time.Saturday, time.Sunday: | |
fmt.Println("It's the weekend") | |
default: | |
fmt.Println("It's a weekday") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ("fmt" "net/http") | |
func handler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello, World!") | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8080", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
// Worker function that does some work | |
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
for job := range jobs { | |
fmt.Printf("Worker %d started job %d\n", id, job) | |
time.Sleep(time.Second) // Simulate work | |
fmt.Printf("Worker %d finished job %d\n", id, job) | |
results <- job * 2 // Simulated result | |
} | |
} | |
func main() { | |
const numJobs = 5 | |
const numWorkers = 3 | |
jobs := make(chan int, numJobs) | |
results := make(chan int, numJobs) | |
var wg sync.WaitGroup | |
// Start workers | |
for w := 1; w <= numWorkers; w++ { | |
wg.Add(1) | |
go worker(w, jobs, results, &wg) | |
} | |
// Send jobs to workers | |
for j := 1; j <= numJobs; j++ { | |
jobs <- j | |
} | |
close(jobs) // No more jobs to send | |
// Wait for all workers to finish | |
wg.Wait() | |
close(results) | |
// Collect results | |
for result := range results { | |
fmt.Printf("Result: %d\n", result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment