alt
codecamp
Golang Essentials: Custom Types
Previous
Next
☰
About
Profile
Account
Login
Sign Up
Logout
Run
Test
Mark Complete
## Custom Types and Structs in Go In Go, you can define your own types and create complex data structures using structs. ### Custom Types A custom type is defined using the `type` keyword. It can be based on existing types: ```go type UserID int ``` ### Structs A struct is a collection of fields. It's used to group data together to form records. #### Defining Structs Use the `type` keyword followed by the struct name and the keyword `struct`: ```go type Person struct { Name string Age int } ``` #### Using Structs To create an instance of a struct, use the struct name followed by field values: ```go p := Person{Name: "Alice", Age: 30} ``` ### Methods on Structs You can define methods on structs: ```go func (p Person) Greet() string { return "Hello, my name is " + p.Name } ``` ### Conclusion Custom types and structs are powerful features in Go that allow you to define complex data types and structures, making your code more organized and expressive.