1590901320
Welcome to this course on Go Programming Language Tutorial. Go is an open source programming language which was originally developed by Google. In this Go Tutorial we will Learn Go from the Basics with Code Examples. Go is a statically-typed language. Go has a syntax similar to C. Go has built-in concurrency.
Subscribe to the channel https://www.youtube.com/watch?v=O_LyJ0roqas
#golang
1599854400
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
1653395160
Repeat
Go implementation of different backoff strategies useful for retrying operations and heartbeating.
Let's imagine that we need to do a REST call on remote server but it could fail with a bunch of different issues. We can repeat failed operation using exponential backoff policies.
Exponential backoff is an algorithm that uses feedback to multiplicatively decrease the rate of some process, in order to gradually find an acceptable rate.
The example below tries to repeat operation 10 times using a full jitter backoff. See algorithm details here.
// An example operation that do some useful stuff.
// It fails five first times.
var last time.Time
op := func(c int) error {
printInfo(c, &last)
if c < 5 {
return repeat.HintTemporary(errors.New("can't connect to a server"))
}
return nil
}
// Repeat op on any error, with 10 retries, with a backoff.
err := repeat.Repeat(
// Our op with additional call counter.
repeat.FnWithCounter(op),
// Force the repetition to stop in case the previous operation
// returns nil.
repeat.StopOnSuccess(),
// 10 retries max.
repeat.LimitMaxTries(10),
// Specify a delay that uses a backoff.
repeat.WithDelay(
repeat.FullJitterBackoff(500*time.Millisecond).Set(),
),
)
The example of output:
Attempt #0, Delay 0s
Attempt #1, Delay 373.617912ms
Attempt #2, Delay 668.004225ms
Attempt #3, Delay 1.220076558s
Attempt #4, Delay 2.716156336s
Attempt #5, Delay 6.458431017s
Repetition process is finished with: <nil>
The example below is almost the same as the previous one. It adds one important feature - possibility to cancel operation repetition using context's timeout.
// A context with cancel.
// Repetition will be cancelled in 3 seconds.
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
time.Sleep(3 * time.Second)
cancelFunc()
}()
// Repeat op on any error, with 10 retries, with a backoff.
err := repeat.Repeat(
...
// Specify a delay that uses a backoff.
repeat.WithDelay(
repeat.FullJitterBackoff(500*time.Millisecond).Set(),
repeat.SetContext(ctx),
),
...
)
The example of output:
Attempt #0, Delay 0s
Attempt #1, Delay 358.728046ms
Attempt #2, Delay 845.361787ms
Attempt #3, Delay 61.527485ms
Repetition process is finished with: context canceled
Let's imagine we need to periodically report execution progress to remote server. The example below repeats the operation each second until it will be cancelled using passed context.
// An example operation that do heartbeat.
var last time.Time
op := func(c int) error {
printInfo(c, &last)
return nil
}
// A context with cancel.
// Repetition will be cancelled in 7 seconds.
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
time.Sleep(7 * time.Second)
cancelFunc()
}()
err := repeat.Repeat(
// Heartbeating op.
repeat.FnWithCounter(op),
// Delay with fixed backoff and context.
repeat.WithDelay(
repeat.FixedBackoff(time.Second).Set(),
repeat.SetContext(ctx),
),
)
The example of output:
Attempt #0, Delay 0s
Attempt #1, Delay 1.001129426s
Attempt #2, Delay 1.000155727s
Attempt #3, Delay 1.001131014s
Attempt #4, Delay 1.000500428s
Attempt #5, Delay 1.0008985s
Attempt #6, Delay 1.000417057s
Repetition process is finished with: context canceled
The example below is almost the same as the previous one but it will be cancelled using special error timeout. This timeout resets each time the operations return nil.
// An example operation that do heartbeat.
// It fails 5 times after 3 successfull tries.
var last time.Time
op := func(c int) error {
printInfo(c, &last)
if c > 3 && c < 8 {
return repeat.HintTemporary(errors.New("can't connect to a server"))
}
return nil
}
err := repeat.Repeat(
// Heartbeating op.
repeat.FnWithCounter(op),
// Delay with fixed backoff and error timeout.
repeat.WithDelay(
repeat.FixedBackoff(time.Second).Set(),
repeat.SetErrorsTimeout(3*time.Second),
),
)
The example of output:
Attempt #0, Delay 0s
Attempt #1, Delay 1.001634616s
Attempt #2, Delay 1.004912408s
Attempt #3, Delay 1.001021358s
Attempt #4, Delay 1.001249459s
Attempt #5, Delay 1.004320833s
Repetition process is finished with: can't connect to a server
Author: ssgreg
Source Code: https://github.com/ssgreg/repeat
License: MIT license
1596440100
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
.
if/else
conditional statementGo 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
.
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
1596793260
http
package to create and initialize HTTPS servers in Go.(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.
(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.
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
1599287850
Golang array is a fixed-size collection of items of the same type. The items of an array are stored sequentially and can be accessed using their index. If we want to declare an array in Go, a programmer specifies the type of the elements and the number of items required by an array.
Golang programming language provides a data structure called an** array**, which can store the fixed-size sequential collection of items of the same type.
The array is used to store the collection of data, but it is often more useful to think of the array as the collection of variables of the same type.
Instead of declaring individual variables, such as no1, no2, …, and no99, you declare one array variable such as numbers and use no[0], no[1], and …, no[99] to represent individual variables.
#golang #go #golang array #golang programming