Go/Strings: Difference between revisions
From charlesreid1
No edit summary |
(→Slices) |
||
| Line 9: | Line 9: | ||
Indexing a string does not access characters - it accesses the individual bytes. So, when you store a character value in a string, you are storing the byte representation of that character at that point in time. | Indexing a string does not access characters - it accesses the individual bytes. So, when you store a character value in a string, you are storing the byte representation of that character at that point in time. | ||
==Slices== | ==String Slices== | ||
See the [[Go/Arrays]] page for notes on how array slices work. | |||
Summary: array slices are data values themselves, and are not just pointers. So when we pass a slice to a function, we are passing a copy of the slice data structure. (The slice contains a pointer, but is not a pointer itself.) | |||
The slice passed as an argument to the function is itself a copy of the original slice. That means that if we use the slice variable passed into the function to modify values of the array being referred to by the slice data structure, we change the actual, underlying array data. But if we modify the slice variable itself, recall that the slice variable is a simple data structure, so a copy of that simple data structure is what's passed into the function. If that variable (which is a copy) is modified, it won't modify the original slice variable/data structure. | |||
<pre> | |||
func SubtractOneFromLength(slice []byte) []byte { | |||
slice = slice[0 : len(slice)-1] | |||
return slice | |||
} | |||
func main() { | |||
... | |||
fmt.Println("Before: len(slice) =", len(slice)) | |||
newSlice := SubtractOneFromLength(slice) | |||
fmt.Println("After: len(slice) =", len(slice)) | |||
fmt.Println("After: len(newSlice) =", len(newSlice)) | |||
} | |||
</pre> | |||
Output: | |||
<pre> | |||
Before: len(slice) = 50 | |||
After: len(slice) = 50 | |||
After: len(newSlice) = 49 | |||
Program exited. | |||
</pre> | |||
=Flags= | =Flags= | ||
{{GoFlag}} | {{GoFlag}} | ||
Revision as of 18:26, 12 December 2018
Related: Rosalind/Problem 1A
The Go blog: strings, bytes, runes, and characters in Go: https://blog.golang.org/strings
How Strings Work
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. So, when you store a character value in a string, you are storing the byte representation of that character at that point in time.
String Slices
See the Go/Arrays page for notes on how array slices work.
Summary: array slices are data values themselves, and are not just pointers. So when we pass a slice to a function, we are passing a copy of the slice data structure. (The slice contains a pointer, but is not a pointer itself.)
The slice passed as an argument to the function is itself a copy of the original slice. That means that if we use the slice variable passed into the function to modify values of the array being referred to by the slice data structure, we change the actual, underlying array data. But if we modify the slice variable itself, recall that the slice variable is a simple data structure, so a copy of that simple data structure is what's passed into the function. If that variable (which is a copy) is modified, it won't modify the original slice variable/data structure.
func SubtractOneFromLength(slice []byte) []byte {
slice = slice[0 : len(slice)-1]
return slice
}
func main() {
...
fmt.Println("Before: len(slice) =", len(slice))
newSlice := SubtractOneFromLength(slice)
fmt.Println("After: len(slice) =", len(slice))
fmt.Println("After: len(newSlice) =", len(newSlice))
}
Output:
Before: len(slice) = 50 After: len(slice) = 50 After: len(newSlice) = 49 Program exited.
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
|