Start learning free

LESSON 7 OF 338 · WELCOME TO GO

Strings vs Numbers in Go: Types and Quotes

Lesson Read the full explanation

  • Go distinguishes between numbers and strings.


  • 15 is a number. "15" is a string because it is surrounded by quotes.


  • We can do math with numbers, but not with strings that only look like numbers.




Full lesson:

Types tell Go what kind of value we are working with.

var age int = 29
var phone string = "2025550153"

age is a number, so it has type int. phone is text, so it has type string, even though it contains digits.


Assignment
Follow the Coding Tutorial and make age a number and phone a string.


Hint
Numbers do not use quotes. Strings use quotes.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · WELCOME TO GO

    Strings Vs Numbers in Go, explained

    Why the difference matters

    Go separates text from numbers. The value 15 is a number. The value "15" is a string, because it sits between double quotes. They look similar when printed, but they are different kinds of values, and Go treats them differently.

    The type of a value decides what you can do with it. You can add, subtract and multiply numbers. You can join strings and measure their length. Go does not let you mix the two without saying so.

    Types in a declaration

    When you declare a variable with var, you write its type after the name:

    var age int = 29
    var phone string = "2025550153"
    

    age is an int, a whole number, so the value has no quotes. phone is a string, so its value is in double quotes, even though every character is a digit. The type on the left and the kind of value on the right have to agree.

    What happens when they don't agree

    Go checks this when it compiles, before anything runs. If you put a string where an int belongs, or the other way round, the build stops with a message like these:

    cannot use "29" (untyped string constant) as int value in variable declaration
    cannot use 2025550153 (untyped int constant) as string value in variable declaration
    

    Both messages say the same thing: the value on the right has the wrong type for the variable. The fix is to change the value, or the declared type, so that they match.

    Why the same digits behave differently

    Numbers support arithmetic. Strings that contain digits do not turn into numbers by themselves. The + operator shows the difference clearly:

    fmt.Println(15 + 4)     // 19
    fmt.Println("15" + "4") // 154
    

    With numbers, + adds. With strings, + joins the text, so "15" + "4" is the three characters 154. Mixing the two kinds, as in "15" + 4, does not compile.

    Which one should a value be?

    A good question to ask is whether you would ever calculate with the value. An age, a price or a count is a quantity, so it should be a number. A phone number, a postal code or an ID is a label made of digits. You never add two phone numbers, and a leading zero matters in a label: "007" is a string of three characters, while a number has no way to remember how many zeros you typed. Labels belong in strings.

    Mixing the two kinds

    Go never converts between them for you. With age := 29 and text := "15", both of these fail to compile:

    invalid operation: text + 4 (mismatched types string and untyped int)
    invalid operation: age + text (mismatched types int and string)
    

    When you do need to cross over, you say so with the strconv package: strconv.Atoi("15") turns text into an int (it also returns an error value, which is nil when the text is a valid number), and strconv.Itoa(15) turns an int into the string "15". After converting the text with Atoi, n + 4 gives 19. After Itoa, strconv.Itoa(15) + "4" gives 154. A later lesson covers conversions in detail.

    If you are ever unsure what type a value has, fmt.Printf("%T", value) prints it: %T shows int for 15 and string for "15".

    The exercise, step by step

    The starter code has both declarations the wrong way round:

    var age int = "29"
    
    var phone string = 2025550153
    
    1. Fix age. The declared type is int, so the value must be a number. Remove the quotes: 29.
    2. Fix phone. The declared type is string, so the value must be text. Add double quotes: "2025550153".

    Neither type changes. Only the value does. The result:

    var age int = 29
    
    var phone string = "2025550153"
    

    Key takeaways

    • Quotes make a value a string; no quotes make it a number.
    • In var name type = value, the value must match the type.
    • Go reports a mismatch at compile time, so the program doesn't run.
    • + adds numbers but joins strings.
    • Use numbers for quantities you calculate with and strings for identifiers such as phone numbers.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free