Go中的类型和减法

huangapple go评论115阅读模式
英文:

Types and subtraction in Go

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Go编程语言的初学者,对这个示例程序感到非常困惑。

通过以下方式执行程序:
$ go run <filename>.go 1(或任何其他整数)

我的问题如下:

请关注程序中的以下行:

digit := stringOfDigits[column] - '0'

如果我运行

fmt.Println(stringOfDigits[column])

这将返回50


如果我运行

fmt.Println(digit)

这将返回2

那么,为什么50 - '0'等于2?

为什么stringOfDigits[column]返回50?50是怎么进去的?

还有,'0'和"0"之间有什么区别,引号是否重要?

在程序中,'0'会运行,而"0"会导致程序出错。

代码块:

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)

func main() {
    if len(os.Args) == 1 {
        fmt.Printf("usage: %s <whole-number>\n", filepath.Base(os.Args[0]))
        os.Exit(1)
    }

    stringOfDigits := os.Args[1]
    for row := range bigDigits[0] {
        line := ""
        for column := range stringOfDigits {
            digit := stringOfDigits[column] - '0'
            if 0 <= digit && digit <= 9 {
                line += bigDigits[digit][row] + "  "
            } else {
                log.Fatal("invalid whole number")
            }
        }
        fmt.Println(line)
    }
}

var bigDigits = [][]string{
    {"  000  ",
     " 0   0 ",
     "0     0",
     "0     0",
     "0     0",
     " 0   0 ",
     "  000  "},
    {" 1 ", "11 ", " 1 ", " 1 ", " 1 ", " 1 ", "111"},
    {" 222 ", "2   2", "   2 ", "  2  ", " 2   ", "2    ", "22222"},
    {" 333 ", "3   3", "    3", "  33 ", "    3", "3   3", " 333 "},
    {"   4  ", "  44  ", " 4 4  ", "4  4  ", "444444", "   4  ",
        "   4  "},
    {"55555", "5    ", "5    ", " 555 ", "    5", "5   5", " 555 "},
    {" 666 ", "6    ", "6    ", "6666 ", "6   6", "6   6", " 666 "},
    {"77777", "    7", "   7 ", "  7  ", " 7   ", "7    ", "7    "},
    {" 888 ", "8   8", "8   8", " 888 ", "8   8", "8   8", " 888 "},
    {" 9999", "9   9", "9   9", " 9999", "    9", "    9", "    9"},
}

希望对你有帮助!

英文:

I am a beginner to the Go programming language and I am pulling my hair out over this example program.

Execute the program by
$ go run &lt;filename&gt;.go 1 (or any other whole number)

My question is as follows:

Please focus on the following line in the program

digit := stringOfDigits[column] - &#39;0&#39;

If I run

fmt.Println(stringOfDigits[column])

this returns 50


If I run

fmt.Println(digit)

this returns 2

So, How is 50 - '0' equal to 2?

And why does stringOfDigits[column] return 50? Like how did 50 even get in there?

And also, what is the difference between '0' and "0", do quotation marks matter?

In the program, '0' will run where as "0" will break the program

Code block:

package main
import (
&quot;fmt&quot;
&quot;log&quot;
&quot;os&quot;
&quot;path/filepath&quot;
)
func main() {
if len(os.Args) == 1 {
fmt.Printf(&quot;usage: %s &lt;whole-number&gt;\n&quot;, filepath.Base(os.Args[0]))
os.Exit(1)
}
stringOfDigits := os.Args[1]
for row := range bigDigits[0] {
line := &quot;&quot;
for column := range stringOfDigits {
digit := stringOfDigits[column] - &#39;0&#39;
if 0 &lt;= digit &amp;&amp; digit &lt;= 9 {
line += bigDigits[digit][row] + &quot;  &quot;
} else {
log.Fatal(&quot;invalid whole number&quot;)
}
}
fmt.Println(line)
}
}
var bigDigits = [][]string{
{&quot;  000  &quot;,
&quot; 0   0 &quot;,
&quot;0     0&quot;,
&quot;0     0&quot;,
&quot;0     0&quot;,
&quot; 0   0 &quot;,
&quot;  000  &quot;},
{&quot; 1 &quot;, &quot;11 &quot;, &quot; 1 &quot;, &quot; 1 &quot;, &quot; 1 &quot;, &quot; 1 &quot;, &quot;111&quot;},
{&quot; 222 &quot;, &quot;2   2&quot;, &quot;   2 &quot;, &quot;  2  &quot;, &quot; 2   &quot;, &quot;2    &quot;, &quot;22222&quot;},
{&quot; 333 &quot;, &quot;3   3&quot;, &quot;    3&quot;, &quot;  33 &quot;, &quot;    3&quot;, &quot;3   3&quot;, &quot; 333 &quot;},
{&quot;   4  &quot;, &quot;  44  &quot;, &quot; 4 4  &quot;, &quot;4  4  &quot;, &quot;444444&quot;, &quot;   4  &quot;,
&quot;   4  &quot;},
{&quot;55555&quot;, &quot;5    &quot;, &quot;5    &quot;, &quot; 555 &quot;, &quot;    5&quot;, &quot;5   5&quot;, &quot; 555 &quot;},
{&quot; 666 &quot;, &quot;6    &quot;, &quot;6    &quot;, &quot;6666 &quot;, &quot;6   6&quot;, &quot;6   6&quot;, &quot; 666 &quot;},
{&quot;77777&quot;, &quot;    7&quot;, &quot;   7 &quot;, &quot;  7  &quot;, &quot; 7   &quot;, &quot;7    &quot;, &quot;7    &quot;},
{&quot; 888 &quot;, &quot;8   8&quot;, &quot;8   8&quot;, &quot; 888 &quot;, &quot;8   8&quot;, &quot;8   8&quot;, &quot; 888 &quot;},
{&quot; 9999&quot;, &quot;9   9&quot;, &quot;9   9&quot;, &quot; 9999&quot;, &quot;    9&quot;, &quot;    9&quot;, &quot;    9&quot;},
}

答案1

得分: 8

&#39;0&#39; 是一个具有值 48rune 字面量规范 中提到:

> rune 字面量表示一个 rune 常量,它是一个标识 Unicode 代码点的整数值。rune 字面量由一个或多个字符包围而成。

字面量的 rune 也是 无类型常量

> 常量可以是有类型或无类型的。字面量常量、true、false、iota 以及仅包含无类型常量操作数的某些常量表达式都是无类型的。
>
> 常量可以通过常量声明或转换显式地给定类型,也可以在变量声明、赋值或表达式中隐式地获得类型。

这意味着在这个表达式中:

digit := stringOfDigits[column] - &#39;0&#39;

常量 &#39;0&#39; 和结果变量 digit 都将从表达式 stringOfDigits[column] 隐式地获得它们的类型,该表达式的类型为 byteuint8 的别名),因为 stringOfDigits 是一个 string

digit 变量的值为 byte(2) 的唯一方式是 stringOfDigit[column] 的值为 byte(50),也可以写作 byte(&#39;2&#39;)

这就是为什么 50 - &#39;0&#39; 的结果是 2

英文:

&#39;0&#39; is a rune literal with value 48. The specification says:

> A rune literal represents a rune constant, an integer value identifying a Unicode code point. A rune literal is expressed as one or more characters enclosed in single quotes.

Literal runes are also untyped constants:

> Constants may be typed or untyped. Literal constants, true, false, iota, and certain constant expressions containing only untyped constant operands are untyped.
>
> A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression.

Which means that in this expression:

digit := stringOfDigits[column] - &#39;0&#39;

both the constant &#39;0&#39; and the resulting variable digit will acquire their type implicitly from the expression stringOfDigits[column], which has type byte (an alias of uint8) because stringOfDigits is a string.

The only way for digit to be byte(2), is for stringOfDigit[column] to be byte(50), which may also be spelled as byte(&#39;2&#39;).

That's how 50 - &#39;0&#39; becomes 2.

答案2

得分: 6

&#39;0&#39;的值是一个字符,不等于0&#39;0&#39;实际上是一个rune字面量,它由一个等于48的单个字符(字节)组成。stringOfDigits[column]是一个byte类型。

当处理单个字符时,它将与ASCII表匹配。

为了获得正确的结果,你可以从每个字节值中减去48,假设你只处理0-9的单个数字:

stringOfDigits[column] - 48 - (&#39;0&#39; - 48)

我假设你想忽略空格。空格等于32,所以你可以使用如下条件:

if stringOfDigits[column] == 32 { // 同样的效果: == &#39; &#39;
continue;
}

或者你可以使用strconv包来转换大于9的数字。

英文:

The value &#39;0&#39; is a character and is not equal 0. &#39;0&#39; is actually a rune literal which consists of a single character (byte) that is equal to 48. stringOfDigits[column] is a byte.

When working with a single character, it will match up with an ASCII table.

In order to get the correct results you can subtract 48 from each byte value, assuming that you're only working with 0-9 in single digits:

stringOfDigits[column] - 48 - (&#39;0&#39; - 48)

I'm assuming you want to ignore spaces. A space is equal to 32, so you can do a condition like:

if stringOfDigits[column] == 32 { // Same thing: == &#39; &#39;
continue;
}

Alternatively you can use the strconv package to convert a number larger than 9.

huangapple
  • 本文由 发表于 2013年8月8日 07:26:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/18115458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定