Learn

We’ve talked about numeric types so far, but Go offers a few other built-in types. One particularly useful type is called a string. A string is Go’s type for storing and processing text. In a general programming sense, “string” is a term for text of any length, and the name is chosen to evoke a sequence of data.

Below, we declared two string variables:

var nameOfSong string var nameOfArtist string

Afterward, we assign a value to the variables with the = sign. Surround the text you want to store with double-quotation marks (i.e., ", the single-quote ' has a special other meaning and isn’t used to define strings):

nameOfSong = "Stop Stop" nameOfArtist = "The Julie Ruin"

You can use the + operator on strings to join them, this is known as string concatenation. Note that + does not add in additional spaces or punctuation.

var description string description = nameOfSong + " is by: " + nameOfArtist + "." fmt.Println(description) // Prints "Stop Stop is by: The Julie Ruin.

As mentioned before, strings are very useful, so let’s practice using them in our own code.

Instructions

1.

In main.go, create a string variable called favoriteSnack but do not assign a value just yet.

2.

Assign a value to favoriteSnack, make it whatever snack is your favorite!

3.

Using string concatenation, print out the message "My favorite snack is " followed by whatever the value inside of favoriteSnack is.

Be sure to add the trailing space to "My favorite snack is " or else it will render as "My favorite snack isbroccoli" instead of "My favorite snack is broccoli".

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?