Menu
Coddy logo textTech

Strings

Part of the Fundamentals section of Coddy's GO journey — lesson 10 of 109.

Strings in Go are used to store text data. Let's learn how to work with them step by step.

First, let's create a string variable using the var keyword with an explicit type declaration:

var message string = "Hello, Go!"
fmt.Println(message)

After executing this code, the text Hello, Go! will show on the screen. We've created a string variable named message and displayed its value.

Now, let's create a string using Go's short declaration syntax with :=, which automatically infers the type:

greeting := "Welcome to Go programming"
fmt.Println(greeting)

After executing this code, the text Welcome to Go programming will show on the screen. The := operator creates a new variable and determines its type based on the value provided.

Both methods accomplish the same thing - they create string variables that store text. The short declaration syntax (:=) is more commonly used in Go because it's concise and convenient.

challenge icon

Challenge

Beginner

In this challenge, you'll practice working with strings in Go.

We've already set up two string variables: firstName and lastName. Your task is to:

  1. Create a third string variable called greeting with the value "Hello, my name is " (note the trailing space after "is")
  2. Print each of the three strings on separate lines using fmt.Println()

Cheat sheet

Strings in Go store text data. Create string variables using explicit type declaration:

var message string = "Hello, Go!"
fmt.Println(message)

Or use short declaration syntax with := for automatic type inference:

greeting := "Welcome to Go programming"
fmt.Println(greeting)

The := operator is more commonly used as it's concise and convenient.

Try it yourself

package main

import "fmt"

func main() {
	// These variables are already defined for you
	firstName := "Alex"
	lastName := "Smith"
	
	// TODO: Create a greeting string variable with the value "Hello, my name is"
	
	// TODO: Print all three strings (greeting, firstName, and lastName) on separate lines
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals