What Are Structures in Golang

In Go language, a structure is a collection of variables (fields) grouped under a single name. It is a composite data type that allows us to create custom data structures to hold related information. Structures in Go are like classes in object-oriented programming like C, and C++, but they do not support inheritance. Instead, they rely on composition to achieve code reuse. This article covers the structures in Golang and how we can declare and access the struct members.

What is a Structure in Golang

In Golang, a structure is a composite data type that consists of zero or more named fields, each of which has a specific type. The fields of a structure can be of any type, including other structures, arrays, functions, or interfaces.

Here is an example of a structure in Golang:

type Person struct {
    FullName string
    YearsOld int
    Location Address
}
type Address struct {
    StreetName  string
    CityName    string
    CountryName string
}

 
Here we have defined a Person structure that has three fields: FullName, YearsOld, and Location. The Location field is itself a structure that has three fields: StreetName, CityName, and CountryName.

How to Declare Struct in Golang

We can declare a struct in Golang using the type keyword. The name of the struct is defined after the type keyword, and its fields are enclosed in curly braces { }. Here’s the syntax for declaring a struct in Go:

type StructName struct {
    FieldName1 FieldType1
    FieldName2 FieldType2
    ...
}

 
Below is an example of how to declare a struct named Person with two fields name and age of types string and int respectively:

type Person struct {
    name string
    age int
}

 
In the above code, we have used the type keyword to declare a new struct named Person with two fields name of type string and age of type int. The fields are separated by a Newline character but a semicolon (;) can also be used for separating them.

How to Access Struct Member in Golang

To access the fields of a struct instance in the Go language dot (“.”) operator is used. This dot operator is followed by a field name. Here’s an example of how to access the name and age fields of a Person struct instance:

// Create a new `Person` struct instance with name "kash" and age 24
kash := Person{name: "kash", age: 24}

// Access the fields of the `kash` struct instance
fmt.Println(kash.name) // Output: "kash"
fmt.Println(kash.age)  // Output: 24

 
In the above code, we have created a new Person struct instance named kash with the name kash and age 24. We then access the name and age fields of the kash struct instance using the (“.”) operator and print them to the console.

It’s important to note that the fields of a struct instance are accessed using dot notation and not the arrow notation (->) used in some other programming languages. The dot notation is used consistently across Go for accessing fields of structs, as well as properties and methods of other types.

Example Code of Declaring and Accessing the Struct Member in Golang

Below is a complete example of declaring the Person struct in Go and printing its values to the screen:

package main
import "fmt"
type Person struct {
    name string
    age int
}
func main() {
    // Create a new `Person` struct instance with name "kash" and age 24
    kash := Person{name: "kash", age: 24}
    // Print the `name` and `age` of `kash` struct instance to the console
    fmt.Printf("Name: %s\n", kash.name)
    fmt.Printf("Age: %d\n", kash.age)
}

 
In the above-written code, we first declared the Person struct. This struct contains two fields that are name and age. After that, we created a new Person struct instance named kash with the name kash and age 24.

To display name and age fields, we use the fmt.Printf function with the %s and %d format specifiers to print the name and age fields, respectively.

After running, code following output appears on the console:

How to Pass Struct as Function Arguments

To pass a struct as a function argument in Go, we simply have to specify the struct type as the parameter type in the function signature, and then pass the struct instance as an argument when calling the function.

Example Code

The below example shows how to pass a Person struct instance as an argument to a function in Go language and print its values to the screen:

package main
import "fmt"
// Declare a struct named `Person` with two fields: `name` and `age`
type Person struct {
    name string
    age int
}
// Declare a function named `printPerson` that takes a `Person` struct as an argument
func printPerson(p Person) {
    fmt.Printf("Name: %s\n", p.name)
    fmt.Printf("Age: %d\n", p.age)
}
func main() {
    // Create a new `Person` struct instance with name "kash" and age 24
    kash := Person{name: "kash", age: 24}
    // Pass the `kash` struct instance to the `printPerson` function
    printPerson(kash)
}

 
In the above code, we first declared the Person struct with two fields, name, and age. We then declare a function named printPerson that takes a Person struct as an argument and prints its name and age fields to the screen using the fmt.Printf function.

In the main function, we created a new Person struct instance named kash with the name kash and age 24. We then pass the kash struct instance to the printPerson function by calling the printPerson function and passing kash as the argument.

The following output can be seen on the console after running the above code:

Conclusion

In Golang, structures can represent complex data types and encapsulate related data. A structure is a data type made up of one or more fields, each of which is given a specific name and type. The fields of a structure can be of any type, including other structures, arrays, functions, or interfaces. This article discussed Go structures in detail, for more info on declaring and accessing the struct elements read the article.



from https://ift.tt/dn3soBV

Post a Comment

0 Comments