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
BeginnerIn 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:
- Create a third string variable called
greetingwith the value"Hello, my name is "(note the trailing space after "is") - Print each of the three strings on separate lines using
fmt.Println()
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"
// Note: Do not add any extra spaces.
// TODO: Print all three strings (greeting, firstName, and lastName) on separate lines
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making DecisionsPractice on your own: Online Go compiler