当前位置:网站首页>[golang] quick review guide quickreview (I) -- string

[golang] quick review guide quickreview (I) -- string

2022-06-23 20:22:00 DDGarfield

During the Spring Festival last year , A sudden outbreak , Let bloggers learn for a few days Golang, Since then, it has been lack of use . I saw some Golang Project source code , Find yourself right Golang It seems a little unfamiliar , Ebbinghaus memory forgetting curve tells us : Be diligent in reviewing . To deepen your memory , As another C# The frequency of use is very high .NET developer , We are going to adopt :C# And Golang Comparison of , With Golang Mainly ,C# Review series supplemented by . Since it's review , The face will not cover all , So this series is not a tutorial , If you want to learn systematically , Bloggers can recommend .

golang Compared with other languages (C#,python etc. ), Grammar sugar is relatively small . Everybody knows C# and python A lot of grammar sugar , especially python, Sometimes I read the source code written by others , Not all can be read quickly , Even a few developers wrote it differently , and Golang Different ,Golang As long as we stick to laying a solid foundation , You can read the source code , Even understand , So we need to lay a solid foundation ( This also shows that the foundation of bloggers is not solid ).

String- character string

1.C# String

The string is in C# in , It's a special type , It cannot be simply summed up as a value type , Or reference type . There are two things to remember :

  • 1. Whatever you do with strings , Will generate a new instance in memory , Even a simple reassignment operation .
  • 2.string A string can be treated as a read-only array .
string name="randyfield";
char name_0=name[0];
name[0]="R";// error : Inside is an indexer ,public char this[int index] { get; }, read-only 

2.Golang String

The string is in Golang in ,string The bottom is through byte Array implementation of . Chinese characters in unicode Take up 2 Bytes , stay utf-8 Code down 3 Bytes , Not much else , The only thing to notice is the character :

  • If it's English characters , Just use byte, The essence is a int8 type , Used to deal with ascii character
  • If it's in Chinese 、 Japanese or other matching characters are used rune, It's really a int32 type , Used to deal with unicode or utf-8 character

3. String flip

Finally, let's consolidate , Use the two codes to realize string flipping respectively , The effect is as follows :"RandyField I like to eat phoenix tail !"---"! Tail Phoenix eats happily dleiFydnaR"

1 C# Realization

string Reverse(string str)
{
    // Convert to array 
    char[] nameArray = str.ToCharArray();
    for (int i = 0; i < nameArray.Length / 2; i++)
    {
        char temp;
        temp = nameArray[i];
        nameArray[i] = nameArray[nameArray.Length - 1 - i];
        nameArray[nameArray.Length - 1 - i] = temp;
    }
    return new string(nameArray);
}

C# It provides us with a lot of api, It could be easier

 static string StringReverse(string str)
 {
      return new string(str.ToCharArray().Reverse().ToArray());
 }

2 Golang Realization

func Reverse(str string) string {
    // Convert to slices 
 strSlice := []rune(str)
 for i := 0; i < len(strSlice)/2; i++ {
  var temp rune
  temp = strSlice[i]
  strSlice[i] = strSlice[len(strSlice)-1-i]
  strSlice[len(strSlice)-1-i] = temp
 }
 return string(strSlice)
}

Use Golang The property of multiple assignment of , Throw the dirty work to the compiler , The code can be simpler :

func reverse(str string) string {
    // Convert to slices 
 strSlice := []rune(str)
 for i, j := 0, len(strSlice)-1; i < j; i, j = i+1, j-1 {
  strSlice[i], strSlice[j] = strSlice[j], strSlice[i]
 }
 return string(strSlice)
}

emphasize : This series is not a tutorial , If you want to learn systematically , Bloggers can recommend learning resources .

原网站

版权声明
本文为[DDGarfield]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231850564634.html