当前位置:网站首页>【Golang】快速复习指南QuickReview(一)——字符串string
【Golang】快速复习指南QuickReview(一)——字符串string
2022-06-23 18:52:00 【DDGarfield】
去年春节期间,突如其来的疫情,让博主学习了几天Golang,之后便一直缺乏使用。之前看到一些Golang项目的源码,发现自己对Golang好像有点不熟,艾宾浩斯记忆遗忘曲线告诉我们:要勤于复习。为了加深自己的记忆,又作为一个C#使用频率超高的.NET开发者,预备在接下来采用:C#与Golang的对比,以Golang为主,C#为辅的复习系列。既然是复习,面不会全部覆盖,所以这个系列并不是教程,如果想系统的学习,博主可推荐。
golang相对于其他语言(C#,python等),语法糖相对较少。大家都知道C#和python的语法糖很多,特别是python,有时候读别人写过的源码,不一定都能快速读懂,甚至几个开发人员编写的都不一样,而Golang不同,Golang只要坚持打牢基础,就能阅读源代码,甚至读懂,所以需要打牢基础(这也说明博主基础并不牢靠)。
String-字符串
1.C#的字符串
字符串在C#中,是一个特殊的类型,不能简单把它归纳为值类型,或者引用类型。需要记住的有两点:
- 1.无论对字符串做什么操作,都会在内存中生成一个新的实例,即使是一个简单的重新赋值操作。
- 2.string字符串可以看作一个只读数组。
string name="randyfield";
char name_0=name[0];
name[0]="R";//错误:内部是一个索引器,public char this[int index] { get; },只读的
2.Golang的字符串
字符串在Golang中,string底层是通过byte数组实现的。中文字符在unicode下占2个字节,在utf-8编码下占3个字节,其他没多少内容,唯一需要注意的就是字符:
- 如果是英文字符,就用
byte,实质是一个int8类型,常用来处理ascii字符 - 如果是中文、日文或者其他符合字符就用
rune,实在是一个int32类型,常用来处理unicode或utf-8字符
3.字符串翻转
最后来巩固一下,用两者代码分别实现字符串翻转,效果如下:"RandyField 喜欢吃凤尾!"---"!尾凤吃欢喜 dleiFydnaR"
1 C#实现
string Reverse(string str)
{
//转换为数组
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# 为我们提供了很多api,还可以更简单
static string StringReverse(string str)
{
return new string(str.ToCharArray().Reverse().ToArray());
}
2 Golang实现
func Reverse(str string) string {
//转换为切片
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)
}
使用Golang的多重赋值的特性,把脏活累活扔给编译器,代码可以更简单:
func reverse(str string) string {
//转换为切片
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)
}
强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。
边栏推荐
- UGeek大咖说 | 可观测之超融合存储系统的应用与设计
- Is it safe to make new debt
- Stochastic process -- Markov chain
- 打新债 要求 打新债安全吗
- 火线沙龙第26期-多云安全专场
- Development of block hash quiz game system (DAPP)
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- Kubernetes resource topology aware scheduling optimization
- LeetCode 473. 火柴拼正方形
- 开源 SPL 重新定义 OLAP Server
猜你喜欢

Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181

Naacl 2022 finds | byte proposes MTG: multilingual text generation data set

Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation

FlagAI飞智:AI基础模型开源项目,支持一键调用OPT等模型

20省市公布元宇宙路线图

Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage

35 year old crisis? It has become a synonym for programmers

80% of people will be wrong about the three counter intuitive questions?

活动报名 | MongoDB 5.0 时序存储特性介绍

准备好迁移上云了?请收下这份迁移步骤清单
随机推荐
Kotlin jetpack compose Tab的渲染 AnimatedVisibility的使用
开源 SPL 重新定义 OLAP Server
解读2022年度敏捷教练行业现状报告
Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3
Gaussdb (DWS) database intelligent monitoring operation and maintenance service - node monitoring indicators
MySQL时间函数的运用,简单问题
UGeek大咖说 | 可观测之超融合存储系统的应用与设计
SAVE: 软件分析验证和测试平台
Daily question brushing record (II)
Chaos engineering, learn about it
The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
[cloud trends] the four highlights of Huawei cloud store brand new release are here
[one by one series] identityserver4 (VIII) uses entityframework core to persist data
Save: software analysis, verification and test platform
How to install SSL certificates in Microsoft Exchange 2010
活动报名 | MongoDB 5.0 时序存储特性介绍
Stochastic process -- Markov chain
打新债有条件吗 打新债安全吗
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage