Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of string Application in Go

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this "string application case analysis in Go" article, so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "string application case analysis in Go" article.

The essence of a string

Strings play an important role in programming languages. There are generally two types of data structures behind strings:

One that specifies the length at compile time and cannot be modified

One has a dynamic length that can be modified.

For example, like strings in Python, strings in Go cannot be modified and can only be accessed.

In Python, if you change the value of a string, you get the following result:

> hi = "Hello" > > hi'Hello' > hi [0] = 'h'Traceback (most recent call last): File ", line 1, in TypeError:' str' object does not support item assignment >

In the same way, the same is true in Go:

Package mainimport "fmt" func main () {var hello = "Hello" hello [1] = 'h'fmt.Println (hello)} / / # command-line-arguments// string_in_go/main.go:8:11: cannot assign to hello [1] (strings are immutable)

There are two ways to terminate a string:

One is the implicit declaration of the C language, with the character "\ 0" as the Terminator

One is the explicit declaration of the Go language

The string representation structure of the Go language is as follows:

Type StringHeader struct {Data uintptr / / Data points to the underlying character array Len int / / Len to represent the length of the string}

A string is essentially an array of characters, each of which corresponds to one or more integers when stored. Use these integers to represent characters, such as printing a byte array of hello:

Package mainimport "fmt" func main () {var hello = "Hello" for I: = 0; I < len (hello); iTunes + {fmt.Printf ("% x", hello [I])}} / / Output: underlying principle of 48 65 6c 6c 6f strings

A string has a special identity and can be declared in two ways:

Var S1 string = `hello world`

Var S2 string = "hello world"

String constants are eventually marked as Token of type StringLit during the lexical parsing phase and passed to the next stage of compilation.

In the parsing phase, the UTF-8 character is read recursively, and the single apostrophe or double quotation mark is the identity of the string.

The logic of the analysis is in the syntax/scanner.go file:

Func (s * scanner) stdString () {ok: = trues.nextch () for {if s.ch = ='"'{s.nextch () break} if s.ch ='\'{s.nextch () if! s.escape ('"') {ok = false} continue} if s.ch = ='\ n' {s.errorf ("newline in string") ok = falsebreak} if s.ch < 0 {s.errorAtf (0) "string not terminated") ok = falsebreak} s.nextch ()} s.setLit (StringLit, ok)} func (s * scanner) rawString () {ok: = trues.nextch () for {if s.ch = ='`{s.nextch () break} if s.ch < 0 {s.errorAtf (0, "string not terminated") ok = falsebreak} s.nextch ()} / / We leave CRs in the string since they are part of the// literal (even though they are not part of the literal// value) .s.setLit (StringLit) Ok)}

As you can see from the above code, there are two kinds of string checking in Go: one is that the standard string is defined in double quotes, such as "Hello,World", and the other is the original string, defined with\\, so there are two parsing functions for the two strings:

If it is an apostrophe, call the rawString function

If it is a double quotation mark, call the stdString function

The above is about the content of this article on "string application example analysis in Go". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report