当前位置:网站首页>[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 aint8type , Used to deal withasciicharacter - If it's in Chinese 、 Japanese or other matching characters are used
rune, It's really aint32type , Used to deal withunicodeorutf-8character
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 .
边栏推荐
- 数字化采购转型解决方案:SaaS采购管理平台推进企业阳光采购
- Save: software analysis, verification and test platform
- 硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
- Interpreting the 2022 agile coaching industry status report
- GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
- 教你如何用网页开发桌面应用
- 直播分享| 腾讯云 MongoDB 智能诊断及性能优化实践
- Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
- Syntaxe des requêtes fédérées SQL (inline, left, right, full)
- 如何避免基因领域“黑天鹅”事件:一场预防性“召回”背后的安全保卫战
猜你喜欢

Real topic of the 13th National Competition of single chip microcomputer in the Blue Bridge Cup

Why is only one value displayed on your data graph?

GL Studio 5 安装与体验

直播分享| 腾讯云 MongoDB 智能诊断及性能优化实践

重庆 奉节耀奎塔,建成后当地连中五名进士,是川江航运的安全塔

解读2022年度敏捷教练行业现状报告

35岁危机?内卷成程序员代名词了…

SQL聯合查詢(內聯、左聯、右聯、全聯)的語法

After the collapse of UST, will the stable currency market pattern usher in new opportunities?

火线沙龙第26期-多云安全专场
随机推荐
Ugeek's theory 𞓜 application and design of observable hyperfusion storage system
混沌工程,了解一下
JDBC 在性能测试中的应用
GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
科班出身,结果外包都不要
Stochastic process -- Markov chain
Naacl 2022 finds | byte proposes MTG: multilingual text generation data set
力扣每日一练之字符串Day6
每日刷题记录 (二)
教你如何用网页开发桌面应用
JDBC 在性能測試中的應用
基于SSM实现微博系统
【Golang】快速复习指南QuickReview(一)——字符串string
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
LeetCode 260. Number III that appears only once
35岁危机?内卷成程序员代名词了…
35 year old crisis? It has become a synonym for programmers
Live sharing | Tencent cloud mongodb intelligent diagnosis and Performance Optimization Practice
Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
[golang] quick review guide quickreview (VIII) -- goroutine