?‍♂️ What is 'go get' command in Go
GOSAMPLES
by
1y ago
In Go, to add a new external package to your project, you must first download it. You can do this by using the built-in go get command. The go get command is a tool used to download dependencies along with updating the go.mod file. It is part of the Go toolchain and comes pre-installed with the language. Using this command, you can download Go packages for your project from remote repositories such as GitHub, Bitbucket or other Git-based repositories. Important note The latest versions of the go get command work only with Go modules. It means that go get will not work if your project is not a ..read more
Visit website
? How to update all Go packages to the latest version
GOSAMPLES
by
1y ago
As a quick reference, to upgrade the Go modules in your project, you can use the following commands: Use go get -u ./... if you want to update all dependencies to the latest version in the current directory and its subdirectories. Use go get -t -u ./... to update all dependencies in the current directory and its subdirectories including test dependencies. Use go get -u all if you want to update all packages in the main module and all its dependencies including test dependencies. Use go get -u to update all dependencies in the current directory only. After running the go get command, it is a g ..read more
Visit website
? Join URL path elements in Go
GOSAMPLES
by
1y ago
In web development, a common task is to combine different URL elements into one, for example, if you are developing a REST API client and want to concatenate a given hostname with relative paths to endpoints. In Go since version 1.19 this is very easy, just use the url.JoinPath() function from the net/url package. Alternatively, you can also use the JoinPath() method of the URL struct from the same package. These functions can help you avoid common errors that can arise when manually concatenating path elements, for example, double slashes or missing slashes in the final URL, which can cause u ..read more
Visit website
? Write end-to-end tests in Go using httptest.Server
GOSAMPLES
by
1y ago
Many programming languages, including Go, have frameworks for performing HTTP end-to-end (e2e) tests. However, not everyone knows that such tests can be performed using the httptest package built into the Go standard library. In this short article, we will show you how to do it - we will prepare end-to-end tests for a simple endpoint using only the httptest.Server from the httptest package. End-to-end (e2e) testing is a type of software testing that aims to verify the functionality and behavior of an application from start to finish, simulating real-world scenarios. In the case of an HTTP ser ..read more
Visit website
? Get local IP address in Go
GOSAMPLES
by
1y ago
In today’s world of cloud computing and distributed systems, networking plays a critical role. One of the most common tasks in networking is to obtain the local outbound IP address of a machine. This can be useful for various reasons, such as identifying your machine on a network, checking for network connectivity, or accessing external services that require your IP address. In this blog post, we will explore how to get a local outbound IP address in Go application and explain the different methods available. Local IP address vs Public IP address Before we start, let’s recap what the differenc ..read more
Visit website
? Generate UUID in Go
GOSAMPLES
by
1y ago
UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify information in computer systems. UUIDs are generated using algorithms that guarantee that each of them is unique. They are commonly used in programming languages to uniquely identify objects or data. UUIDs can be represented in various formats, but the most commonly used is the hyphenated format, which looks like this: 53aa35c8-e659-44b2-882f-f6056e443c99 In Go, there are many packages with which you can generate a UUID. Some of the more popular ones are github.com/google/uuid and github.com/gofrs/uuid. Both o ..read more
Visit website
? Convert date or time to string in Go
GOSAMPLES
by
1y ago
To convert time.Time to a string in Go, you can use the Time.String() method, which outputs the time in a default format, or Time.Format() method if you need a custom format. Look at the example below where we convert the current time in UTC timezone: t := time.Now().UTC() to a string. The s1 string is created by calling the t.String() method, and as you can see in the output, it is formatted using the default format: 2006-01-02 15:04:05.999999999 -0700 MST. On the other hand, the second string s2 is formatted by t.Format() method with the year-month-day format as an argument: 2006-01-02. If ..read more
Visit website
✨ 5 different ways to loop over a time.Ticker in Go
GOSAMPLES
by
1y ago
The time.Ticker is a very popular structure in Go that provides clock ticks at fixed intervals. Internally, it contains a channel that delivers subsequent ticks. Using this channel, we can create several different variants of the loop depending on the needs, for example, whether the loop should start immediately, whether we need a ticker value, or whether we want to stop the ticker properly while the program is running. All these cases will be described in this article ?. Simple loop over a time.Ticker When you want to perform some task every fixed period of time, use the standard for loop ove ..read more
Visit website
? Go Generics cheatsheet
GOSAMPLES
by
1y ago
Getting started Generics release Generics in Go are available since the version 1.18, released on March 15, 2022. Generic function With Generics, you can create functions with types as parameters. Instead of writing separate functions for each type like: func LastInt(s []int) int { return s[len(s)-1] } func LastString(s []string) string { return s[len(s)-1] } // etc. you can write a function with a type parameter: func Last[T any](s []T) T { return s[len(s)-1] } Type parameters are declared in square brackets. They describe types that are allowed for a given function: Generic fun ..read more
Visit website
? Count the occurrences of an element in a slice in Go
GOSAMPLES
by
1y ago
To count the elements in a slice in Go that satisfy a certain condition, it is best to use Generics available since Go 1.18. With it, we can create a function that takes as arguments a slice of any type and a filter function that returns true or false for an element of that slice. In this way, we get a universal function that, for any slice, counts the occurrences of elements that meet any condition. package main import ( "fmt" "strings" ) func count[T any](slice []T, f func(T) bool) int { count := 0 for _, s := range slice { if f(s) { count++ } } return count } func main() { s ..read more
Visit website

Follow GOSAMPLES on FeedSpot

Continue with Google
Continue with Apple
OR