Go (Golang) Semaphore Pattern Tutorial

Semaphores are a synchronization mechanism not new to computer science and present in other programming languages other than Go. With Semaphores we can synchronize access to multiple resources or in the episode we are going to see today, we are rate-limiting or throttling a set of concurrent workers to a pre-defined fixed capacity.

In this episode we are going to look at our we can limit the number of concurrent go routines running by using a buffered channel first and later on a packaged solution developed by the Go team (golang.org/x/sync/semaphore).

Buffered channels are the perfect solution for this problem as they are a tool to communicate safely between concurrent workers in go.

Buffered Channels in Go - https://tour.golang.org/concurrency/3
Semaphore package - https://pkg.go.dev/golang.org/x/sync/semaphore

Source Code (with golang.org/x/sync/semaphore) - https://play.golang.org/p/7pJTSOQIZ1M
Source Code (with buffered channel) - https://play.golang.org/p/T0XfI09QHvv

πŸ’Ό Golang Cafe - https://golang.cafe
πŸ“¬ Golang Cafe Jobs Newsletter - https://golang.cafe/newsletter
🐦 Golang Cafe Twitter - https://twitter.com/golangcafe
πŸ“£ Telegram Channel - https://t.me/golangcafe

#golang #go #semaphore pattern

What is GEEK

Buddha Community

Go (Golang) Semaphore Pattern Tutorial

Go (Golang) Semaphore Pattern Tutorial

Semaphores are a synchronization mechanism not new to computer science and present in other programming languages other than Go. With Semaphores we can synchronize access to multiple resources or in the episode we are going to see today, we are rate-limiting or throttling a set of concurrent workers to a pre-defined fixed capacity.

In this episode we are going to look at our we can limit the number of concurrent go routines running by using a buffered channel first and later on a packaged solution developed by the Go team (golang.org/x/sync/semaphore).

Buffered channels are the perfect solution for this problem as they are a tool to communicate safely between concurrent workers in go.

Buffered Channels in Go - https://tour.golang.org/concurrency/3
Semaphore package - https://pkg.go.dev/golang.org/x/sync/semaphore

Source Code (with golang.org/x/sync/semaphore) - https://play.golang.org/p/7pJTSOQIZ1M
Source Code (with buffered channel) - https://play.golang.org/p/T0XfI09QHvv

πŸ’Ό Golang Cafe - https://golang.cafe
πŸ“¬ Golang Cafe Jobs Newsletter - https://golang.cafe/newsletter
🐦 Golang Cafe Twitter - https://twitter.com/golangcafe
πŸ“£ Telegram Channel - https://t.me/golangcafe

#golang #go #semaphore pattern

Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Anatomy of Conditional Statements and Loops in Go

Go provides if/else and switch conditional statements for code execution based on certain conditions. To execute some code over and over again, we have the for loop.

The if/else conditional statement

Go provides if, if-else, if-else if-else variants of if/else statement we are familiar with. It is used to check a condition, and execute some code when the condition is true or false.

The if condition

Simple use of if condition is demonstrated below. Unlike most of the programming languages, Go does not allow to wrap the condition inside parenthesis ().

#golang #programming #golang-tutorial #go-tutorial #go

Zander  Herzog

Zander Herzog

1596793260

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go.

Image for post

(source: unsplash.com)

In the β€œSimple Hello World Server” lesson, we learned about net/http package, how to create routes and how [ServeMux](https://golang.org/pkg/net/http/#ServeMux) works. In the β€œRunning multiple HTTP servers” lesson, we learned about [Server](https://golang.org/pkg/net/http/#Server) structure and how to run multiple HTTP servers concurrently.

In this lesson, we are going to create an HTTPS server using both Go’s standard server configuration and custom configuration (using [_Server_](https://golang.org/pkg/net/http/#Server) structure). But before this, we need to know what HTTPS really is?

HTTPS is a big topic of discussion in itself. Hence while writing this lesson, I published an article just on β€œHow HTTPS works?”. I advise you to read this lesson first before continuing this article. In this article, I’ve also described the encryption paradigm and SSL certificates generation process.


If we recall the simplest HTTP server example from previous lessons, we only need http.``[ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) function to start an HTTP server and http.``[HandleFunc](https://golang.org/pkg/net/http/#HandleFunc) to register a response handler for a particular endpoint.

Image for post

(https://play.golang.org/p/t3sOenOYAzS)

In the example above, when we run the command go run server.go , it will start an HTTP server on port 9000. By visiting http://localhost:9000 URL in a browser, you will be able to see a Hello World! message on the screen.

Image for post

(http://localhost:9000)

As we know, the nil argument to ListenAndServe() call invokes Go to use the [DefaultServeMux](https://golang.org/pkg/net/http/#DefaultServeMux) response multiplexer, which is the default instance of ServeMux structure provided globally by the Go. The HandleFunc() call adds a response handler for a specific route on the multiplexer instance.

The http.ListenAndServe() call uses the Go’s standard HTTP server configuration, however, in the previous lesson, how we can customize a server using [Server](https://golang.org/pkg/net/http/#Server) structure type.

To start an HTTPS server, all we need do is to call ServerAndListenTLS method with some configuration. Just like ServeAndListen method, this method is available on both the http package and the Server structure.

The http.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#ListenAndServeTLS) method uses the Go’s standard server implementation, however, both [Server](https://golang.org/pkg/net/http/#Server) instance and Server.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#Server.ListenAndServeTLS) method can be configured for our needs.

#go-programming-language #go #golang-tutorial #go-programming #golang

Common Anti-Patterns in Go

A list of anti-patterns you should avoid while writing code in Golang.

It has been widely acknowledged that coding is an art, and like every artisan who crafts wonderful art and is proud of them, we as developers are also really proud of the code we write. In order to achieve the best results, artists constantly keep searching for ways and tools to improve their craft. Similarly, we as developers keep leveling up our skills and remain curious to know the answer to the single most important question β€” β€˜How to write good code.’

Frederick P. Brooks in his book β€˜The Mythical Man Month: Essays on Software Engineering’ wrote:

β€œThe programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.”

xkcd 844: Good Code (Randall Munroe)

Image source:  https://xkcd.com/844/

This post tries to explore answers to the big question mark in the comic above. The simplest way to write good code is to abstain from including anti-patterns in the code we write.

#golang development #golang tutorials #golang #go