Start learning free

LESSON 2 OF 338 · WELCOME TO GO

Introduction to Strings in Go: Text in Double Quotes

Lesson Read the full explanation

TL ; DR:

  • In Go, a string is text written inside double quotes, like "Hello world".


  • Be careful not to forget a quote. This code is missing a closing quote:

    fmt.Println("Welcome!)
    

    and would produce a syntax error.



Full lesson:

We can use text in Go to express meaningful messages.

In programming, a string is a sequence of characters: letters, numbers, spaces, and symbols.

For example, we can print strings with fmt.Println():

fmt.Println("My name is Andy")
fmt.Println("Welcome to AlgoCademy!")

The output is:

My name is Andy
Welcome to AlgoCademy!

The quotes are not printed. They only tell Go where the string starts and ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Use double quotes around the text.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · WELCOME TO GO

    Introduction To Strings in Go, explained

    What a string is

    A string is a sequence of characters: letters, digits, spaces and symbols. Programs use strings for anything that is text, such as a name, a message for the user or a line read from a file. In Go, a string is written between double quotes:

    "Hello world"
    

    The quotes are not part of the text. They only tell Go where the string starts and where it ends. That's why printing a string shows its contents without them:

    fmt.Println("My name is Andy")
    fmt.Println("Welcome!")
    
    My name is Andy
    Welcome!
    

    Syntax and examples

    Strings can hold anything

    Between the quotes you can put digits, spaces, punctuation and even an apostrophe. All of these are valid:

    fmt.Println("2024")
    fmt.Println("Let's code!")
    fmt.Println("")
    

    The first prints the four characters 2024; they are text, not a number. The last one is the empty string, which prints an empty line.

    Joining strings

    The + operator on two strings joins them, which is called concatenation:

    fmt.Println("Go" + " " + "rocks")
    fmt.Println("9" + "1")
    
    Go rocks
    91
    

    The second line shows the difference between text and numbers: "9" + "1" is two characters stuck together, while 9 + 1 would be the number 10.

    Escape sequences and raw strings

    A backslash inside a double-quoted string starts an escape sequence. \n is a newline, \t is a tab, and \" puts a double quote inside the text without ending the string:

    fmt.Println("He said \"hi\"")
    fmt.Println("tab\there")
    
    He said "hi"
    tab	here
    

    Go also has raw strings in backticks. Nothing inside them is an escape sequence, and they can span several lines:

    fmt.Println(`raw\n`)
    
    raw\n
    

    Length and characters

    A Go string is a sequence of bytes, and the text is encoded as UTF-8. For plain English letters, one character is one byte, but accented letters and many symbols take more:

    fmt.Println(len("hello"))
    fmt.Println(len("héllo"))
    
    5
    6
    

    len counts bytes, not visible characters, so the accented é adds two. You will meet the character-level tools, runes, when you work with strings in more depth. Strings are also immutable: you can build a new string, but you can't change a character inside an existing one.

    Strings are one of the basic types in Go, next to integers, floating-point numbers and booleans. Every value has exactly one type, and the type decides what you can do with it: you can join two strings, but you can't add a string to a number without converting one of them first.

    Common mistakes and gotchas

    • A missing closing quote. fmt.Println("Welcome!) never closes the string, so the compiler stops at the end of the line with the error newline in string. A double-quoted string can't continue onto the next line.
    • Single quotes. In Go, single quotes make a single character (a rune), not a string. fmt.Println('I love coding!') fails with more than one character in rune literal. Use double quotes for text.
    • A quote inside the text. "He said "hi"" ends the string early. Escape inner double quotes with a backslash, or wrap the text in backticks.
    • Mixing text and numbers with +. "Age: " + 31 doesn't compile, since Go does not convert a number to text for you. Print them as separate values instead: fmt.Println("Age:", 31).
    • Quotes around something that isn't text. "31" is a string. 31 is a number. They behave differently.

    The exercise

    The task is to print the string I love coding!. Combine what you know about fmt.Println with a string literal:

    fmt.Println("I love coding!")
    

    The string goes inside the parentheses, with the opening quote before I and the closing quote after the exclamation mark. The output is I love coding! with no quotes. Check that the text matches exactly, including the capital I, the single spaces and the !, since a different string prints different output.

    Key takeaways

    • A string is text written in double quotes; the quotes aren't printed.
    • Every opening quote needs a closing quote on the same line.
    • Single quotes are for single characters, not for strings.
    • + joins strings, and "9" + "1" is "91", not 10.
    • Use \" for a quote inside the text and \n for a new line.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free