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...
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...
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...
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 (
...
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"
...
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...
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...
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...
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() ...
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[:])
...
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 ...