本文介绍了golang rune()函数如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个在线发布的函数,该函数在golang中使用了rune()函数,但是我很难查找它的含义.我正在阅读本教程,并且对文档没有经验,因此很难找到我想要的东西.

I came across a function posted online that used the rune() function in golang, but I am having a hard time looking up what it is. I am going through the tutorial and inexperienced with the docs so it is hard to find what I am looking for.

具体来说,我试图了解为什么这失败了...

Specifically, I am trying to see why this fails...

fmt.Println(rune("foo"))

这不是

fmt.Println([]rune("foo"))

推荐答案

rune是Go中的一种类型.它只是int32的别名,但通常用于表示Unicode点. rune()不是函数,它是将类型转换为rune的语法. Go中的转换始终使用type()语法,这可能会使它们看起来像函数.

rune is a type in Go. It's just an alias for int32, but it's usually used to represent Unicode points. rune() isn't a function, it's syntax for type conversion into rune. Conversions in Go always have the syntax type() which might make them look like functions.

代码的第一位失败,因为在Go中未定义将字符串转换为数字类型的操作.但是,在语言规范中,将字符串转换为符文/int32的片段的定义如下:

The first bit of code fails because conversion of strings to numeric types isn't defined in Go. However conversion of strings to slices of runes/int32s is defined like this in language specification:

因此您的示例打印了一片值102、111和111的符文

So your example prints a slice of runes with values 102, 111 and 111

这篇关于golang rune()函数如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 15:58