Golang bytes.Buffer Examples
Examples of Golang bytes.Buffer
The bytes package provides the Buffer type for efficient manipulation of byte slices. A Buffer starts out empty but grows as data of types like string, byte, and []byte are written to it.
...
Formatting float to currency string in Go.
Using localized formatting
Use golang.org/x/text/message to print using localized formatting for any language in the Unicode CLDR.
Make a main.go file containing the following:
package main
import (
"...
Golang path and filepath Examples.
Using filepath.Base
The number is the index of the last slash in the string. If you want to get the file's base name, use filepath.Base:
package main
import (
"fmt"
"path/filepath"
)
func main() {
path ...
An example program using numbers.
Integers And Floating Point Numbers
Go has several different types to represent numbers. Generally we split numbers into two different kinds: integers and floating-point numbers.
Make a main.go file containing the ...
In Golang, there are 2 ways to check if a slice contains an element.
Using contains Function
The following example should cover whatever you are trying to do:
package main
import "fmt"
func Contains[E comparable](s []E, v E) bool {
for _, ...
In Golang, using the sort.Slice function is the easiest way to sort a slice
Using Slice Function
You simply pass an anonymous function to the sort.Slice function:
package main
import (
"fmt"
"sort"
)
func main() {
s := []string{"Houston...
In Golang, there are 2 ways to check string is in json format.
Using Unmarshal Function
The following example should cover whatever you are trying to do:
package main
import (
"encoding/json"
"fmt"
)
func isJson(s string) bool {
var js ...
In Golang, there are 2 ways to duplicate slices.
Using append Function
You could write one simple statement to make a shallow copy of a slice.
The following example should cover whatever you are trying to do:
package main
import "fmt"
type T...
In Golang, using the Unmarshal JSON function is the easiest way to Marshal / Unmarshal json with a custom type attribute.
JSON (JavaScript Object Notation) is a lightweight data-interchange format.
Using UnmarshalJSON Function
Example on custom Unm...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES CBC.
Ehrsam, Meyer, Smith and Tuchman invented the cipher block chaining (CBC) mode of operation in 1976. In CBC mode, each block of plaintext is XORed with the ...
In Golang, using the crypto/aes package is the easiest way to encrypt a string using AES GCM 256.
In cryptography, Galois/Counter Mode (GCM) is a mode of operation for symmetric-key cryptographic block ciphers which is widely adopted for its performa...
In Golang, using the crypto/hmac library is the easiest way to generate a SHA256 HMAC Hash from a string.
In cryptography, an HMAC (hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptograp...
In Golang, using the Sum256 function is the easiest way to get a SHA256 hash from a string.Go implements several hash functions in various crypto/* packages.
Using Sum256 Function
The sha256.Sum256() function returns the SHA256 checksum of the data...
In Golang, using the Sum function is the easiest way to get a MD5 hash from a string.
Using Sum Function
The md5.Sum() function returns the MD5 checksum of the data.
The following example should cover whatever you are trying to do:
package mai...
In Golang, using the IsPrivate function is the easiest way to check if IP address is in private network space.
Using IsPrivate Function
The IsPrivate() function reports whether ip is a private address, according to RFC 1918 (IPv4 addresses) and RFC...