Anonymous Structs
Part of the Fundamentals section of Coddy's GO journey — lesson 100 of 109.
Anonymous structs let you create one-time-use structs without defining a separate type.
Create an anonymous struct directly in your code:
func main() {
// Anonymous struct declaration and initialization
person := struct {
name string
age int
}{
name: "Bob",
age: 25,
}
fmt.Println(person.name, "is", person.age, "years old")
}
The output shows the values from our anonymous struct:
Bob is 25 years oldChallenge
BeginnerIn this challenge, you'll work with anonymous structs in Go. Anonymous structs are structs that are defined without a separate type declaration.
Your task is to create an anonymous struct for a book with fields for title (string) and pages (int), then print its information.
Cheat sheet
Anonymous structs let you create one-time-use structs without defining a separate type.
Create an anonymous struct directly in your code:
person := struct {
name string
age int
}{
name: "Bob",
age: 25,
}
Try it yourself
package main
import "fmt"
func main() {
// TODO: Create an anonymous struct variable called 'book' with fields:
// - title (string): "The Go Programming Language"
// - pages (int): 380
// Print the book information
fmt.Printf("Book: %s, Pages: %d\n", book.title, book.pages)
}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 Actions10Composite Types: Arrays
Introduction to ArraysDeclaring ArraysInitializing ArraysAccessing Array ElementsArray Length with `len`Iterating Over ArraysMulti-dimensional Arrays13Composite Types: Structs
Defining Custom TypesCreating Struct InstancesAccessing Struct FieldsPointers to StructsInitializing StructsEmbedded StructsAnonymous StructsRecap - Custom Data Structures2Variables 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 Decisions