简体   繁体   中英

Types and subtraction in Go

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 <filename>.go 1 (or any other whole number)

My question is as follows:

Please focus on the following line in the program

digit := stringOfDigits[column] - '0'

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 (
    "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"},
}

The value '0' is a character and is not equal 0 . '0' 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 - ('0' - 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: == ' '
    continue;
}

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

'0' 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] - '0'

both the constant '0' 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('2') .

That's how 50 - '0' becomes 2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM