Skip to content

Instantly share code, notes, and snippets.

@amanbolat
Last active March 4, 2024 21:12
Show Gist options
  • Save amanbolat/ec95400dfcd3aaad2bdb2f9e4a25d469 to your computer and use it in GitHub Desktop.
Save amanbolat/ec95400dfcd3aaad2bdb2f9e4a25d469 to your computer and use it in GitHub Desktop.
Load balancing in golang

Round robin example in Go

An example of a round robin algorithm in Go that can be used for load balancing.

package main
import (
"fmt"
"sync/atomic"
)
func main() {
type node struct {}
var nodes []node
var robinCounter uint64
var chosenNode *node
n := uint64(len(nodes))
for i := uint64(0); i < n; i++ {
r := atomic.AddUint64(&robinCounter, 1)
chosenNode = &nodes[r%n]
}
fmt.Println(chosenNode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment