All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Parse Unix Timestamp to time.Time in Go

In Golang, using the time.Unix() function is the easiest way to parse unix timestamp to time.Time Using time.Unix Function You can directly use time.Unix function of time which converts the unix time stamp to UTC. See the following example: pa...
pooriabt

How to Trim Leading and Trailing White Spaces of a String in Go

In Golang, there are 3 ways to trim leading and trailing white spaces of a string. Using strings.TrimSpace Function The easiest way to trim leading and trailing white spaces of a string in Golang. For example, package main import ( "fmt" "...
Tomoki

How to Get the Maximum Value for an Int Type in Go

In Golang, there are 2 ways to get the maximum value for an int type. Using constant values Since integer types use two's complement arithmetic, you can infer the min/max constant values for int and uint. For example, package main import ( "...
Patcher56

How to List all Standard Packages in Go

In Golang, there are 2 ways to list all standard packages. Using packages go install Use the following command to download the repository to the local file system. install go get golang.org/x/tools/go/packages go install golang.or...
aweis

How to Read a Whole File into a String Variable in Go

In Golang, there are 3 ways to read a whole file into a string variable. Using ioutil.ReadFile Function ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads...
aweis

How to Check the Equality of two Slices in Go

In Golang, there are 3 ways to check the equality of two slices. Using bytes.Equal Function Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice. For example, package main...
aweis

How to Delete or Remove keys in a Map in Go

In Golang, using the delete function is the easiest way to delete keys in a map. Using delete Function Syntax The syntax of the delete function is shown below. func delete(m map[Type]Type1, key Type) delete Usage The delete built-in funct...
Patcher56

How to Get the Directory of the Currently Running File in Go

In Golang, there are 3 ways to get the directory of the currently running file.Using os.Executable Function Executable returns the path name for the executable that started the current process. The easiest way to get the directory of the currently ...
Patcher56

How to Pretty-Print Json in Go

In Golang, there are 2 ways to pretty-print json. Using json.MarshalIndent Function MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one...
ada

How to Remove Packages Installed with go get in Go

In Golang, there are 3 ways to remove packages installed with go get. Using go mod tidy go mod tidy ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s p...
ada

How to Get a Slice of Keys from a Map in Go

In Golang, there are 3 ways to get a slice of keys from a map.Using For Range The easiest way to get keys of map in Golang: package main import "fmt" func main() { m := map[string]int{ "a": 1, "b": 1, "c": 1, } // minimize ...
Sambhav Khandelwal

How to Convert String to Bytes in Go

In Golang, there are 2 ways to assign string to bytes. Using byte Function The easiest way to convert string to []byte in Golang: package main import "fmt" func main() { s := "Hello" b := []byte(s) fmt.Printf("%v\n", b) } [72 101 10...
Tomoki

How to Convert Byte Array to String in Go

In Golang, there are 2 ways to convert byte array to string.Using string Function The easiest way to convert []byte to string in Golang: package main import "fmt" func main() { b := []byte{'a', 'b', 'c'} s := string(b[:]) fmt.Printf("%...
Tomoki

How to Concatenate Two Slices in Go

In Golang, there are 2 ways to concatenate two slices.Using append Function The append built-in function appends elements to the end of a slice. You can use append function, it really simple. For example, package main import "fmt" func main(...
aweis

How to Check if a File Exists in Go

In Golang, there are 2 ways to check if a file exists.Using os.Stat Function Stat returns a FileInfo describing the named file. See the following example: package main import ( "errors" "fmt" "os" ) func main() { // since the additio...
aweis