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

How to find the difference between two slices in Go

Comparing two slices in Go.Using map cache Creates a slice of slice values not included in the other given slices. An implementation is shown in the section below. package main import "fmt" // difference returns the elements in `a` that aren't in ...
aweis

How to split a slice into equally sized chunks in Go

Slice chunking in Go.Using for Loop The easiest method involves iterating over the slice and incrementing by the chunk size. An implementation is shown in the section below. package main import "fmt" func chunkSlice(slice []int, chunkSize int) ([]...
Unused

How to use range in Go

Range loop (for-each) patterns.For-each Loop (slice or map) The range form of the for loop iterates over a slice or map. When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the el...
Unused

How to generate a random string in Go

Creating Random Strings of a fixed length in Go. Using rand.Int63 Function Int63 returns a non-negative pseudo-random 63-bit integer as an int64 from the default Source. package main import ( "fmt" "math/rand" "time" ) const letters = "a...
ada

How to reverse a string in Go

Reverse a String in Golang.Using range a range on a string returns a rune which is a codepoint. package main import "fmt" func Reverse(s string) (result string) { for _, v := range s { result = string(v) + result } return } func main() ...
Tomoki

How to format byte size as kilobytes, megabytes in Go

Convert File Size to a human friendly format.Get file size in human-readable units like kilobytes (KB), Megabytes (MB) or GigaBytes (GB) SI (Decimal 1 k = 1,000) A metric prefix is a unit prefix that precedes a basic unit of measure to indicate a mul...
Sambhav Khandelwal

How to get a substring from a string in Go

Extracting substrings in Go.Using the slice syntax A slice is formed by specifying two indices, a low and high bound, separated by a colon: // a[low : high] s := "Hello,世界" // e fmt.Println(s[1:2]) fmt.Println(s[1:]) // Hello,世界 fmt.Println(s[:]) ...
Patcher56

How many keywords are there in Go

List of Golang Keywords. Keywords The following keywords are reserved and may not be used as identifiers. break default func interface select case defer go map struct chan else goto package switch const ...
Sambhav Khandelwal

How to get content from a url in Go

Reading from a url resource in Go.Fetching a URL Go provides a collection of packages, grouped under net, that make it easy to send and receive information through the Internet. Package http provides HTTP client and server implementations. packag...
Patcher56

How to use fmt.Sprintf Function in Go

Format a string without printing in Go.Basics Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler. fmt.Sprintf Sprintf formats according to a format specif...
Sambhav Khandelwal

How to convert string to json in Go

Converting Json to string in Golang.Decoding JSON Into Structs Go offers built-in support for JSON encoding and decoding, including to and from built-in and custom data types. Unmarshal parses the JSON-encoded data and stores the result in the value ...
aweis

How to check if string contains a substring in Go

Go test string contains substring.Using strings.Contains Function How do I check if a string is a substring of another string in Go? Use the function Contains from the strings package. Contains function returns a boolean value. It returns true if the...
Tomoki

How to convert interface to int64 in Go

Interface variable conversion in Golang.Type Assertion To convert interface to int64 in Go, Using Type Assertion. A type assertion provides access to an interface value's underlying concrete value. package main import "fmt" // t := i.(T) // t, ok ...
ada

How to convert interface to string in Go

Type casting an interface to a string in go.Using fmt.Sprintf Function To convert interface to string in Go, use fmt.Sprintf function. Sprintf formats according to a format specifier and returns the resulting string. package main import "fmt" var ...
pooriabt

Hello World in Go

Hello, World! is the first basic program in any programming language.source code package main import "fmt" func main() { fmt.Println("Hello,世界,こんにちは,안녕하세요") } To run the program Go is a compiled language. The Go toolchain converts a source pro...
Tomoki