How to Create a Log in Golang

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, you can use the “os.OpenFile()” function.  The os.Open() is a built-in function that takes a filepath, flag, and file mode as arguments and opens and reads the file. Syntax os.OpenFile(name/path string, flag int, perm FileMode) OpenFile is the generalized open call; most users will use Open or Create instead. … Read more

Golang os.Create() Method

To create a file in Golang, you can use the “os.Create()” function. Go has a built-in os.Create() function that takes the filename as the argument and creates a file with the specified name or an error. Syntax func Create(name string) (*File, error) Parameters The Create() function is an inbuilt function that creates or truncates the … Read more

How to Delete or Remove File in Go

To delete a file in Golang, you can use the “os.Remove()” function. The os.Remove() is a built-in function that removes a file. Provide a filepath to that file, and the function removes that file. It removes the named file or (empty) directory. If there is an error, that will be of type *PathError. PathError means … Read more

How to Convert a String to Byte Array in Golang

To convert a string to a byte array in Golang, you can use the “[]byte()”. The byte() function takes a string as input and returns the array. A byte is an 8-bit unsigned int. You get a slice containing the string’s bytes to convert a string to a byte array.  Example 1 package main import “fmt” … Read more

Golang String: The Complete Guide

Golang Strings are different compared to other programming languages.  Golang Strings Golang string is a slice of bytes. Create a string by enclosing its contents inside ” “(double quotes) and not ‘ ‘(single quotes). The UTF-8 character can be defined in the memory size from 1 byte (ASCII compatible) to 4 bytes. Hence in Go, all … 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. Example package main import ( “fmt” “reflect” “strings” ) func main() { str1 … Read more

Golang Rune: The Complete Guide

ASCII stands for American Standard Code for Information Interchange, a character encoding standard for electronic communication. ASCII codes represent the text in computers, telecommunications equipment, and other devices. In the past years, we dealt with one character set, ASCII. It used 7 bits to represent 128 characters, including upper and lowercase English letters, digits, and … Read more

How to Convert Golang String to Int

Go is statically typed. Every variable has a static type, precisely one type known and fixed at compile time: int, float32, *MyType, []byte, and so on. In Go, data types distinguish one specific kind of data, defining the values you can assign to a type and the operations you can execute. In programming, there are … 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 package … Read more