Go Strings: Difference between revisions
No edit summary |
|||
Line 17: | Line 17: | ||
=TO DEPLETE= | =TO DEPLETE= | ||
=Overview= | ==Overview== | ||
The pre-declared String [[Go Concepts - Lexical Structure#Pre-Declared_Identifiers|type identifier]] is <tt>string</tt>. String values are (possibly empty) sequences of bytes. String values are ''immutable''. | The pre-declared String [[Go Concepts - Lexical Structure#Pre-Declared_Identifiers|type identifier]] is <tt>string</tt>. String values are (possibly empty) sequences of bytes. String values are ''immutable''. | ||
=String Literals= | ==String Literals== | ||
A ''string literal'' is a [[Go Concepts - Lexical Structure#Constants|string constant]] obtained from concatenating a sequence of characters. | A ''string literal'' is a [[Go Concepts - Lexical Structure#Constants|string constant]] obtained from concatenating a sequence of characters. | ||
==Raw String Literals== | ===Raw String Literals=== | ||
Raw string literals are sequences of characters enclosed by backquotes (backticks) <tt>`</tt>. Any other character is taken literally, back slashes have no special meaning and new lines can appear. Carriage return characters inside raw string literals are discarded. The following code: | Raw string literals are sequences of characters enclosed by backquotes (backticks) <tt>`</tt>. Any other character is taken literally, back slashes have no special meaning and new lines can appear. Carriage return characters inside raw string literals are discarded. The following code: | ||
Line 44: | Line 44: | ||
</pre> | </pre> | ||
==Interpreted String Literals== | ===Interpreted String Literals=== | ||
Interpreted string literals are character sequences between double quotes, as in <tt>"example"</tt>. | Interpreted string literals are character sequences between double quotes, as in <tt>"example"</tt>. | ||
Line 50: | Line 50: | ||
Interpreted strings allow escaping (\n or \t). | Interpreted strings allow escaping (\n or \t). | ||
=String Equality= | ==String Equality== | ||
String equality is tested with the <tt>[[Go_Concepts_-_Operators#.3D.3D|==]]</tt> operator: | String equality is tested with the <tt>[[Go_Concepts_-_Operators#.3D.3D|==]]</tt> operator: | ||
Line 62: | Line 62: | ||
</pre> | </pre> | ||
=String Operators and Functions= | ==String Operators and Functions== | ||
==Indexing Operator== | ===Indexing Operator=== | ||
Indexing operator <tt>[[Go Concepts - Operators#.5B.5D|[]]]</tt> returns a <tt>[[Go Integers#byte|byte]]</tt> (<tt>uint8</tt>) | Indexing operator <tt>[[Go Concepts - Operators#.5B.5D|[]]]</tt> returns a <tt>[[Go Integers#byte|byte]]</tt> (<tt>uint8</tt>) | ||
Line 74: | Line 74: | ||
</pre> | </pre> | ||
==Concatenation Operator== | ===Concatenation Operator=== | ||
[[Go Concepts - Operators#.2B|Concatenation operator +]] concatenates two strings together. Since Go is strong typed, using the concatenation operation between a string and an int, for example, won't work, the int won't be automatically converted to string, Java style. | [[Go Concepts - Operators#.2B|Concatenation operator +]] concatenates two strings together. Since Go is strong typed, using the concatenation operation between a string and an int, for example, won't work, the int won't be automatically converted to string, Java style. | ||
Line 82: | Line 82: | ||
</pre> | </pre> | ||
==String Length== | ===String Length=== | ||
<tt>[[Go Built-In Functions Length and Capacity#len.28.29|len()]]</tt> | <tt>[[Go Built-In Functions Length and Capacity#len.28.29|len()]]</tt> | ||
==Conversion of a <tt>byte</tt> to <tt>string</tt>== | ===Conversion of a <tt>byte</tt> to <tt>string</tt>=== | ||
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | <blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;"> | ||
Line 91: | Line 91: | ||
</blockquote> | </blockquote> | ||
==Reading with a <tt>string</tt> with a <tt>Reader</tt>== | ===Reading with a <tt>string</tt> with a <tt>Reader</tt>=== | ||
<font color=red> | <font color=red> |
Revision as of 04:10, 19 August 2023
External
- String types in the language specification https://golang.org/ref/spec#String_types
- String literals in the language specification https://golang.org/ref/spec#String_literals
Internal
Overview
Strings are sequence of bytes represented as Unicode.
Uninitialized variable value: TO check: ""
(empty string).
TO DEPLETE
Overview
The pre-declared String type identifier is string. String values are (possibly empty) sequences of bytes. String values are immutable.
String Literals
A string literal is a string constant obtained from concatenating a sequence of characters.
Raw String Literals
Raw string literals are sequences of characters enclosed by backquotes (backticks) `. Any other character is taken literally, back slashes have no special meaning and new lines can appear. Carriage return characters inside raw string literals are discarded. The following code:
var sl = `Example \n \t ... "something"` fmt.Println(sl);
will produce:
Example \n \t ... "something"
Interpreted String Literals
Interpreted string literals are character sequences between double quotes, as in "example".
Interpreted strings allow escaping (\n or \t).
String Equality
String equality is tested with the == operator:
var s string = "something" if s == "something" then { ... }
String Operators and Functions
Indexing Operator
Indexing operator [] returns a byte (uint8)
Strings are indexed starting with 0. If the index is out of bounds, the runtime generates a run-time panic:
panic: runtime error: index out of range
Concatenation Operator
Concatenation operator + concatenates two strings together. Since Go is strong typed, using the concatenation operation between a string and an int, for example, won't work, the int won't be automatically converted to string, Java style.
"a" + "b"
String Length
Conversion of a byte to string
Reading with a string with a Reader
TODO
strings.NewReader()
See Go_Package_strings#NewReader.28.29