Go/Strings: Difference between revisions
From charlesreid1
No edit summary |
|||
| Line 2: | Line 2: | ||
The Go blog: strings, bytes, runes, and characters in Go: https://blog.golang.org/strings | The Go blog: strings, bytes, runes, and characters in Go: https://blog.golang.org/strings | ||
=Basics= | |||
==How strings work in Go== | ==How strings work in Go== | ||
| Line 7: | Line 9: | ||
A string in Go is a read-only slice of bytes. A string can hold arbitrary bytes, it is not required to hold unicode/UTF-8/other format. That means that "characters" are not special types in Go; rather, strings refer to bytes. | A string in Go is a read-only slice of bytes. A string can hold arbitrary bytes, it is not required to hold unicode/UTF-8/other format. That means that "characters" are not special types in Go; rather, strings refer to bytes. | ||
Indexing a string does not access characters - it accesses the individual bytes | Indexing a string does not access characters - it accesses the individual bytes, the byte representation of the characters. | ||
==String | ==String Package== | ||
To use | The string package provides several useful string methods. To use it, include it in your imports: | ||
<pre> | <pre> | ||
import ( | |||
"strings" | |||
) | |||
</pre> | |||
import | Once you import strings, you can call any of the functions provided by the package. | ||
<pre> | |||
func main() { | func main() { | ||
fmt.Println(strings.ToUpper("Hello world!")) | fmt.Println(strings.ToUpper("Hello world!")) | ||
| Line 67: | Line 73: | ||
</pre> | </pre> | ||
=Flags= | =Flags= | ||
{{GoFlag}} | {{GoFlag}} | ||
Revision as of 21:57, 13 December 2018
Related: Rosalind/Problem 1A
The Go blog: strings, bytes, runes, and characters in Go: https://blog.golang.org/strings
Basics
How strings work in Go
A string in Go is a read-only slice of bytes. A string can hold arbitrary bytes, it is not required to hold unicode/UTF-8/other format. That means that "characters" are not special types in Go; rather, strings refer to bytes.
Indexing a string does not access characters - it accesses the individual bytes, the byte representation of the characters.
String Package
The string package provides several useful string methods. To use it, include it in your imports:
import (
"strings"
)
Once you import strings, you can call any of the functions provided by the package.
func main() {
fmt.Println(strings.ToUpper("Hello world!"))
}
The functions provided by the strings package will then be available via, for example, strings.ToUpper().
To make this a little easier, you can do something analogous to Python's import X as Y:
package main
import s "strings"
func main() {
fmt.Println(s.ToUpper("Hello world!"))
}
List of String Functions
Here is a list of string functions that are available from the string package. These are static methods that operate on string input arguments, they are not methods of the string object itself.
package main
import s "strings"
import "fmt"
var p = fmt.Println
func main() {
p("Contains: ", s.Contains("test", "es"))
p("Count: ", s.Count("test", "t"))
p("HasPrefix: ", s.HasPrefix("test", "te"))
p("HasSuffix: ", s.HasSuffix("test", "st"))
p("Index: ", s.Index("test", "e"))
p("Join: ", s.Join([]string{"a", "b"}, "-"))
p("Repeat: ", s.Repeat("a", 5))
p("Replace: ", s.Replace("foo", "o", "0", -1))
p("Replace: ", s.Replace("foo", "o", "0", 1))
p("Split: ", s.Split("a-b-c-d-e", "-"))
p("ToLower: ", s.ToLower("TEST"))
p("ToUpper: ", s.ToUpper("test"))
p("Len: ", len("hello"))
}
Flags
| Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|