You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: array/README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ To provide a real-world analogy, consider an array of athletes preparing for a s
6
6
7
7
## Implementation
8
8
9
-
In the Go programming language, arrays are considered values rather than pointers and represent the entirety of the array. Whenever an array is passed to a function, a copy is created, resulting in additional memory usage. To avoid this, it is possible to pass a pointer to an array, or use slices instead. The size of the array is constant and it must be known at compile time, and there is no need to use the built-in `make` function when defining arrays.
9
+
In the Go programming language, [arrays](https://go.dev/blog/slices) are considered values rather than pointers and represent the entirety of the array. Whenever an array is passed to a function, a copy is created, resulting in additional memory usage. To avoid this, it is possible to pass a pointer to an array, or use slices instead. The size of the array is constant and it must be known at compile time, and there is no need to use the built-in `make` function when defining arrays.
Copy file name to clipboardExpand all lines: strings/README.md
+78-7
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,12 @@
1
1
# String
2
2
3
-
A string is a ubiquitous data structure, typically a built-in data type in programming languages. However, beneath the surface, strings are essentially arrays of characters that enable textual data storage and manipulation.
3
+
A string is a ubiquitous data structure, typically a built-in data type in programming languages. However, beneath the surface, strings are essentially [slices](../array/) of characters that enable textual data storage and manipulation.
4
4
5
5
## Implementation
6
6
7
-
In Go, strings are a data type. Behind the scenes strings are a slice of bytes. The `strings` package provides several useful convenience functions. Examples include:
7
+
In Go, [strings](https://go.dev/blog/strings) are a data type. Behind the scenes strings are an immutable slice of bytes. Since Go is a UTF-8 compliant language, each character in Go can take up to 4 bytes of storage.
8
+
9
+
The `strings` package provides several useful convenience functions. Examples include:
When a string is iterated in Go using the `range` keyword, every element becomes a [rune](https://blog.golang.org/strings#TOC_5.) which is an alias for the type `int32`. If the code being written works with many single-character strings, it is better to define variables and function parameters as `rune`. The following code shows how to iterate through a string.
18
+
When a iterating every character in a string in Go using the `range` keyword, every element becomes a [rune](https://blog.golang.org/strings#TOC_5.) which is an alias for the type `int32`. If the code being written works with many single-character strings, it is better to define variables and function parameters as `rune` rather than convert them many times. The following code shows how to iterate through a string.
19
+
20
+
```Go
21
+
package main
22
+
23
+
import"fmt"
24
+
25
+
/*
26
+
main outputs the rune (int32) value of each character:
27
+
28
+
Char #0 "a" has value 97
29
+
Char #1 "A" has value 65
30
+
Char #2 "和" has value 21644
31
+
Char #5 "平" has value 24179
32
+
Char #8 "😊" has value 128522
33
+
*/
34
+
funcmain() {
35
+
fori, r:=range"aA𓅚😊" {
36
+
fmt.Printf("Char #%d%q has value %d\n", i, string(r), r)
37
+
}
38
+
}
39
+
```
40
+
41
+
A very common tool to use for manipulating strings in Go is the `fmt.Sprintf` function. This is specially useful when converting many values into a string.
Unlike many other programming languages, in Go [regular expressions](https://golang.org/pkg/regexp/) are [guaranteed](https://swtch.com/~rsc/regexp/regexp1.html) to have O(n) time complexity where n is the length of the input, making them a viable and practical option for pattern matching in a string.
61
+
62
+
Here is an example of how you can find a pattern using regular expressions in Go. Given a string return the string if it contains a fish word. A fish word is a word that starts with `fi` optionally followed by other character(s) and ends with `sh`. Examples include {`fish`, `finish`}.
Strings have the same complexity as [arrays](../array/) and slices in Go.
89
+
Since strings are slices of bytes, the time complexity of string operations should be similar to [arrays](../array/). Reading a character at a given index is O(1), but since strings are immutable modifying them involves creating a new string making it a O(n) operation. Go standard library includes `strings.Builder` for more efficient string building.
90
+
91
+
The space complexity to store a string depends on the type of characters. This following example shows how we can index a string and print the hexadecimal value of every byte in it.
92
+
93
+
```GO
94
+
package main
95
+
96
+
import"fmt"
97
+
98
+
// main Outputs 41 f0 93 85 9a f0 9f 98 8a.
99
+
funcmain() {
100
+
input:="A𓅚😊"
101
+
fori:=0; i < len(input); i++ {
102
+
fmt.Printf("%x", input[i])
103
+
}
104
+
}
105
+
```
35
106
36
-
Unlike many other programming languages, [regular expressions](https://golang.org/pkg/regexp/)are guaranteed to have O(n) time complexity in Go, allowing efficient string pattern matching.
107
+
The output of the above code indicates that 9 bytes are used to store the 3 input characters. 1 byte for the first character and 4 bytes for each of the remaining two.
0 commit comments