Package · Gopher Coding (original) (raw)
Tag: Package
Writing Tests in Go (a Beginners Guide)There a many benefits to testing your code in general, which we won’t go into detail on (but if you’re interested, take a look here) all we will say is the quote below. “Imperfect tests, run frequently, are much better than perfect tests that are never written at all”. - Fowler First Test We’ve created the most basic progam, to add two numbers together, below in main.go which we’re going to write a test for. Tests in Go are found in separate files to the code and are within *_test.go - where * is the filename. So our test would be main_test.go - but if your go file was address.go, you’d create a address_test.go and store them here.
#testing#unit#package#files#function#add
Generating UUIDs in GoUUIDs (Universally Unique Identifiers) serve as a standard for generating identifiers that are unique across time and space. In this blog post, we’ll explore how to generate UUIDs in Go using the google/uuid package. These can be especially useful when generating IDs across different systems - where it wouldn’t be possible to track using an auto incrementing integer.
#uuid#generate#package#id#identifier#string#random#universal
Redis: Connect, Set & Get in GoRedis is a in-memory data store, used a lot in the backend systems of web-apps. Specifically, it’s good at helping with caching and message as it’s high performance lends itself to fast response times at retriving data. In this blog post, we will demonstrate how to connect to Redis using Go and the go-redis library, which is one of the most popular and widely-used libraries for interacting with Redis in Go.
#redis#connection#get#set#goredis#memory#store#package
SHA3 Hash in Golang (256-bit)You can use the golang.org/x/crypto/sha3 [docs] package to perform SHA-3 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using Sprintf. Advantages of Using SHA3 Security: SHA-3 was designed to provide higher security compared to SHA-2 by using a different construction called “sponge construction”, which makes it more resistant to various types of attacks, such as collision and preimage attacks.
#sha3#hash#hexadecimal#sprintf#fmt#crypto#string#package#security
Print Current Memory UsageIn this post we show how you can print memory usage in golang. We do this by outputting the current state of memory at any given time to show how much ram has been allocated and gc cycles made. We have created a function PrintMemUsage() to help out, so you can call this when ever you need to know. All the info we need can be acquired through the runtime [docs] package, which allows us to read the state of the memory into the MemStats struct. It returns stats like how much memory the program is using, how much of it the OS has allocated to it and the number of garbage collections.
#memory#stats#runtime#package#stdlib#memstats#alloc#garbage#gc#os
Convert HEIC to JPG in Go
The “High Efficiency Image File Format” or HEIF is an image format often used by Apple devices. Although called HEIF, the file types are often heic (presumably the ‘c’ stands for container?) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files. In our example, we’re trying to convert these heic files back into jpg files though - so we can use them elsewhere, display them or what ever we choose.
#heic#heif#encoding#images#conversion#file#types#package#apple
Sleeping in Golang
Sleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the time package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided. In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.
#sleep#golang#pause#execution#wait#goroutine#package#unix#time
Install Latest Version of Go (Linux/OSX)
This article describes how to install the latest version of Go (golang). It also helpfully updates itself by pulling the latest version numbers directly, so you don’t have to go and dig out the latest version each time they release a version (yay!) - just copy and paste away. For more info, try their installer docs.
#install#update#linux#osx#mac#latest#version#verison#go#golang#package#deb