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

How to Convert String to Rune in Golang

Understanding Rune in Go. UTF-8 Decoder The unicode/utf8 package provides one that we can use like this: package main import ( "fmt" "unicode/utf8" ) func main() { s := "Hello, 世界" for i := 0; i < len(s); { r, size := utf8.DecodeR...
Unused

How to Use Strings in Golang

Introduction to Strings in Go.What is a String? A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text. Make a main.go file containing the following...
ada

How to Remove Invalid UTF-8 Characters from a String in Go

In Golang, there are 3 ways to remove invalid UTF-8 characters from a string. Using ToValidUTF8 Function The strings.ToValidUTF8() function returns a copy of the string s with each run of invalid UTF-8 byte sequences replaced by the replacement str...
pooriabt

How to Count Characters in a String in Go

In Golang, there are 2 ways to count characters in a string. Using RuneCountInString Function Straight forward natively use the utf8.RuneCountInString() The following example should cover whatever you are trying to do: package main import ( ...
aweis

How to Remove the First Character of a String in Go

In Golang, using the DecodeRuneInString function is the easiest way to remove the first character of a string. Using DecodeRuneInString Function The following example should cover whatever you are trying to do: package main import ( "fmt" ...
Patcher56

How to Index Characters in a String in Go

In Golang, there are 2 ways to index characters in a string. Using individual characters In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value i...
Patcher56

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 an int to a string type in Go

3 ways to convert int64 to string in Golang.Using strconv.Itoa Function The code snippet below shows how to convert an int to a string using the Itoa function. package main import ( "fmt" "strconv" ) func main() { s := strconv.Itoa(100) f...
aweis

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 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