Golang Logging: A Step-by-step Guide

To create a log in Golang, you can use the built-in “log” library. The package log in Go implements the simple logging package that defines a type, Logger, with methods for formatting output. You can use these rough-and-ready logs for local development in which you need to get fast feedback from your code may be … Read more

How to Write a File in Golang

To write a file in Golang, you can use the “io.WriteString()” function. The “os.Create()” function creates or truncates a file and the io.Writer interface provides a way to write data to the file. The os.File type implements the io.Writer interface, so you can use it directly to write data to the file. Example package main … Read more

How to Open a File in Golang

To open a file in Golang, use the “os.OpenFile()” function. Syntax os.OpenFile(name/path string, flag int, perm FileMode) Parameters name: The first parameter is a file name, which is the complete file path of that file. If the file does not exist, and the O_CREATE flag is passed, it is created with mode perm (before umask). … Read more

Golang os.Create() Method

Golang os.Create() function creates a new file if it does not exist, with the file mode 0666 for reading and writing. It truncates the file if it exists, meaning its contents are removed without deleting it. The returned file descriptor is open for reading and writing. If there is some problem, the function returns an error … Read more

How to Delete or Remove File and Directory in Go

To delete a file in Golang, you can “use the os.Remove() function.” If there is an error, that will be of type *PathError. PathError means the program could not find the provided file in a particular path, throwing an error. Syntax func Remove(name string) error Parameters name: It accepts the filename(filepath). Example 1: Removing a … Read more

How to Convert a String to Byte Array in Go

To convert a string to a byte array in Go, you can use the []byte() function. Syntax []byte(input_string) Parameters input_string: We pass a string to the byte slice. Return value It returns the bytes of all the characters of the string. Example 1: Usage of []byte() package main import “fmt” func main() { str := … Read more

How to Convert a Slice to String in Golang

To convert a slice to a string in Golang, you can use the “strings.Join()” function. The conversion doesn’t change the original data. The only difference between string and byte slice is that the strings are immutable, while byte slices can be modified. package main import ( “fmt” “reflect” “strings” ) func main() { str1 := … Read more

What is the rune type in Golang

A rune in Go is a “data type that stores codes that represent Unicode characters.” Unicode is the collection of all possible characters present in the whole world. Golang rune literals are 32-bit integer values (however, they are untyped constants, so their type can change). They represent the Unicode codepoints. “Rune” means Unicode codepoint. (think … Read more

Golang Custom Type Declaration

In Go, you can create “custom types using type declarations”. A custom type is a user-defined type based on an existing one. You can create custom types to make your code more expressive, readable, and easier to maintain. To define a custom type declaration in Golang, you can use the “type” keyword. Example 1: Defining … Read more

Golang Receiver Function

A receiver function in Go is “defined within a type.” The receiver function is associated with the type and can only be called on instances of that type. To define a receiver function, specify the receiver type in parentheses before the function name. The receiver is usually a pointer to the type, allowing you to … Read more

Arrays in Golang

Golang Arrays store multiple values of the same type in a single variable instead of declaring separate variables for each value. How to declare an array in Golang To declare an array in Golang, specify the type of elements and the number of items required by an array. An array belongs to type n[T]. The n … Read more

How to Install Golang on Mac

Follow these steps to install Golang on Mac: Download the latest version of Go for your platform here: https://go.dev/dl/. Follow the instructions for your platform to install the Go tools: https://go.dev/doc/install#install. It is recommended to use the default installation settings. On Mac OS X and Linux, Go is default installed to directory /usr/local/go, and the GOROOT environment … Read more

How to Serialize JSON String in Go

To serialize json string in Go, you can “use the json.Marshal()” function to a slice of bytes. You can deserialize a slice of bytes into a Golang value using the “json.Unmarshal()” function. Syntax func Marshal(v interface{}) ([]byte, error) If Marshal() fails to serialize the input value, it will return a non-nil error. Few things you … Read more

Golang make() Function

Golang make() function is “used for slices, maps, or channels.” The make() function allocates memory on the heap and initializes and puts zero or empty strings into values. The make() function assigns the underlying array with a size equal to the given capacity and returns the slice, which refers to the underlying array. Syntax func … Read more

Golang maps

Golang Maps is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update or delete with the help of keys. Declaration and initialization of Maps in Go map[KeyType]ValueType KeyType may be any comparable type (more on this later), and ValueType may be any type, … Read more

TypeCasting in Golang

Typecasting in Go refers to the process of converting a value of one type to another type. This conversion can be done automatically or explicitly by the programmer. For example, you can typecast long to int to save the long value into a simple integer. Using the cast operator, you can convert the values from one … Read more

How to Convert String to Int64 in Golang

To convert string to int64 in Golang, you can “use the strconv.ParseInt() method”. The strconv.ParseInt() function is used to convert a “decimal string (base 10) and checks that it fits into a 64-bit signed integer”. Example 1: Using ParseInt() function to convert string to int64 package main import ( “fmt” “strconv” ) func main() { … Read more

How to Convert JSON to Map in Golang

To convert a json to a map in Go, use the “json.Unmarshal()” function and pass the JSON string as “bytearray” and the “address of an empty map” as arguments to the function. The json.Unmarshal() method parses the JSON-encoded data and stores the result in the value pointed to by the interface. If an interface is … Read more

Golang fmt.Sprintf() Function

Golang fmt.Sprintf() function is “used to format according to a format specifier and returns the resulting string.” Syntax func Sprintf(format string, a …interface{}) string Parameters format string: This includes some variables along with some strings. a …interface{}: This is the specified constant variable. Return Value The Sprintf() function returns a String and does not print the … Read more

How to Copy an Array into Another Array in Golang

Golang does not provide a specific built-in function to copy one array into another array. But we can create a copy of an array by simply assigning an array to a new variable by value or by reference. Syntax // creating a copy of an array by value arr := arr1 // Creating a copy … Read more

How to Convert String to Rune in Go

To convert a string to a rune in Golang, you can use the “[]rune()”. A rune is a single Unicode code point. A string is a sequence of runes. Syntax []rune(s) Example package main import ( “fmt” ) func main() { // Define a string s := “Hi, Golang!” // Convert the string to a … Read more

How to Convert String to Float in Golang

To convert a string to float in Go, you can use the “strconv.ParseFloat()” function. This function takes two arguments: the string to be parsed and the bit size (32 or 64) of the float to be returned. If the string cannot be converted to a float (for example, if it contains non-numeric characters), the function … Read more

How to Convert JSON to CSV in Golang

To convert JSON to CSV in Golang, decode the JSON data with the json.Decoder and encoding/csv package to write the output CSV data using csv.Writer. First, you will need to unmarshal the JSON data into a suitable data structure (like a slice of structs or a slice of maps), and then you can write that … Read more

Golang Encryption Decryption: How to Create AES Encryption

Encryption is the process of converting the plaintext to ciphertext. In simpler terms, encryption takes readable data and alters it to appear random. Encryption requires an encryption key: a set of mathematical values that both the sender and the receiver of the encrypted message know. If you have seen the movie Imitation Game, you will … Read more

How to Make HTTP Requests (GET / POST) in Golang

To make HTTP requests in Go, you can use the “net/http” package from the standard library. You will make a GET request using the default Go HTTP client. Then, you will enhance your program to make a POST request with a body. GET requests To make a GET request, you can use the “http.Get()” function. … Read more