Go Structures

Published:2021-01-29
Share:

Go Structures are a user-defined data type available in Golang, which allows you to combine different values types (including structures) into one type, its similar to Classes in other object-oriented programming languages, Assume you want to keep track of the articles in a blog. You might want to track the following attributes of each article −

Title
Body
Category
Author

Defining a Structure To define a structure, you must use type and struct statements. The struct statement defines a new data type, with multiple properties or members for your program. The type statement binds a name with the type which is struct in our case. The format of the struct statement is as follows −

type struct_name struct {
   member_definition
   member_definition
   ...
   member_definition
}

Once a structure type is defined, it can be used to declare variables of that type using the following syntax. variable_name := struct_name {value1, value2...}

Accessing Structure Members

Dot or period (.) is used to access struct members, as you can see in the following example:

package main

import "fmt"

type Article struct {
   Title string
   Body string
   Category string
   Author string
}

func main() {

   article := Article{
   Title: "Go Structures",
   Body: "Go Structures are..",
   Category: "learn-golang",
   Author: "coderme.com",
   }
   
 
   // print article details 
   fmt.Println( "Article.Title =", article.Title)
   fmt.Println( "Article.Body =", article.Body)
   fmt.Println( "Article.Category =", article.Category)
   fmt.Println( "Article.Author =", article.Author)
   
}

When the above code is compiled and executed, it produces the following result −

Article.Title = Go Structures
Article.Body = Go Structures are..
Article.Category = learn-golang
Article.Author = coderme.com

Structures as Argument for functions

You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the same way as you did in the above example:

package main

import "fmt"

type Article struct {
   Title string
   Body string
   Category string
   Author string
}

func main() {
   article := Article{
   Title: "Golang for DEVOPS",
   Body: "Golang is useful for DEVOPS ..",
   Category: "learn-golang",
   Author: "coderme.com",
   }
   
   // print article details
   printArticle(article)

}

func printArticle( article Article ) {
   fmt.Println( "Article.Title =", article.Title)
   fmt.Println( "Article.Body =", article.Body)
   fmt.Println( "Article.Category =", article.Category)
   fmt.Println( "Article.Author =", article.Author)
}

When the above code is compiled and executed, it produces the following result −

Article.Title = Golang for DEVOPS
Article.Body = Golang is useful for DEVOPS ..
Article.Category = learn-golang
Article.Author = coderme.com

Pointers to Structures

You can define pointers to structures in the same way as you define pointer to any other variable as follows:

var struct_pointer *Article

or if you prefer in one go

  struct_pointer := &Article{
              Title: "",
              ...
              }

Struct members on a struct pointer can be accessed the same way you'd access a regular struct, by dot (period) operator.

fmt.Println(struct_pointer.Title);

We can rewrite the previous example using pointers as follows:

ppackage main

import "fmt"

type Article struct {
   Title string
   Body string
   Category string
   Author string
}

func main() {
   article := &Article{
   Title: "Golang for DEVOPS",
   Body: "Golang is useful for DEVOPS ..",
   Category: "learn-golang",
   Author: "coderme.com",
   }
   
   // print article details
   printArticle(article)

}

func printArticle( article *Article ) {
   fmt.Println( "Article.Title =", article.Title)
   fmt.Println( "Article.Body =", article.Body)
   fmt.Println( "Article.Category =", article.Category)
   fmt.Println( "Article.Author =", article.Author)
}

Will the output of program be the same? :)

Read also

Go Strings

Strings, which are widely used in Go programming, are a readonly slice of bytes. In the Go programming language, strings are slices. Go standard library provides many libraries to work with strings. strings regexp unicode Creating Strings in Go To create a string you use douple quotes "" not a single quote, however you can use backtick ` for raw strings \n will be printed as \n not as a newline in raw strings, in the following snippet we use double qoutes: welco...

Go Range

The range keyword is used to iterate over elements of an slice, array, map or channel. range over Arrays and Slices Use one variable to get the index of an element. Use two varaibles to get both index and the value of an element. range over Maps Use one variable to get the key of an element. Use two varaibles to get both key and the value of an element. range over Channels Use one variable to get the value of an element. How to use range ? ...

C Recursion

Recursion in programming is the process of calling a procedure repeatedly in a self-similar way, a recursive function or routine is a function that call itself repeatedly. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an...

Using Linux Debian As Router Setup

In this articles series you will learn how to setup Linux (Debian) as a Router for home/business local networks, to spice things I''ll show how to add dns server, proxy/web caching server, and basic web traffic filter to our cute debian box.   In this series I''ll go over: Introduction . Hardware Requirements . Software installation . Basic Setup . /etc/network/Interfaces Configuration . Unbound dns server setup . DHCP server setup . Squid setup. ...

Howto Setup Postgresql Stream Replication Easy Steps

Recently Postgresql Stream replication become even easier, In this article you'll be shown how to setup stream replication for postgresql 9.3+.   Postgresql Stream Replication stream replication is a feature that let a mirror/replica/standby database to get a stream of changes that have been done on another primary database, to be replicated on as many standby databases as needed.   Use Cases stream replication can be used as follows: As Part of Data protection ...