WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - Structures


The array in Go can store the same type of data, but in the structure we can define different data types for different items.

structure is a collection of data consisting of a series of data of the same type or different types.

Structure represents a record, such as a book record of a library, each of which has the following attributes:

  • Title: Title
  • Author: Author
  • Subject: Subject
  • ID: Book ID

Defining Structures

Structure definitions require the use of type and struct statements. A struct statement defines a new data type with one or more members in the structure. The type statement sets the name of the structure. The format of the structure is as follows:

type struct_variable_type struct {
   member definition
   member definition
   ...
   member definition
}

Once the structure type is defined, it can be used for the statement of the variable. The syntax is as follows:

variable_name := structure_variable_type {value1, Value2...valuen}
or 
Variable_name := structure_variable_type { key1: value1, key2: value2...,< keyn: valuen} 

example is as follows:

example

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}


func main() {

    // Create a new structure
    fmt.Println(Books{"Go Language", "www.welookups.com", "Go Language tutorial", 6495407})

    // You can also use key => vAlue format
    fmt.Println(Books{title: "Go Language", author: "www.welookups.com", subject: "Go Language tutorial", book_id: 6495407})

    // Ignored field is 0 or empty
   fmt.Println(Books{title: "Go Language", author: "www.welookups.com"})
}

The output is:

{Go Language www.welookups.com Go Language tutorial 6495407}
{Go Language www.welookups.com Go Language tutorial 6495407}
{Go Language www.welookups.com  0}

Access structure member

If you want to access a structure member, you need to use the dot . operator in the format:

 Structure.Member Name"

Structure type variables are defined using the struct keyword, example is as follows:

example

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* statement Book1 For Books type */
   var Book2 Books        /* statement Book2 For Books type */

   /* book 1 description */
   Book1.title = "Go Language"
   Book1.author = "www.welookups.com"
   Book1.subject = "Go Language tutorial"
   Book1.book_id = 6495407

   /* book 2 description */
   Book2.title = "Python Tutorial"
   Book2.author = "www.welookups.com"
   Book2.subject = "Python Language tutorial"
   Book2.book_id = 6495700

   /* 打印 Book1 information */
   fmt.Printf( "Book 1 title : %s\n", Book1.title)
   fmt.Printf( "Book 1 author : %s\n", Book1.author)
   fmt.Printf( "Book 1 subject : %s\n", Book1.subject)
   fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id)

   /* 打印 Book2 information */
   fmt.Printf( "Book 2 title : %s\n", Book2.title)
   fmt.Printf( "Book 2 author : %s\n", Book2.author)
   fmt.Printf( "Book 2 subject : %s\n", Book2.subject)
   fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id)
}

The above example execution results are:

Book 1 title : Go Language
Book 1 author : www.welookups.com
Book 1 subject : Go Language tutorial
Book 1 book_id : 6495407
Book 2 title : Python Tutorial
Book 2 author : www.welookups.com
Book 2 subject : Python Language tutorial
Book 2 book_id : 6495700

Structure as a function parameter

You can pass a struct type as a parameter to a function like any other data type. And access the structure variable in the above example:

example

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* statement Book1 For Books type */
   var Book2 Books        /* statement Book2 For Books type */

   /* book 1 description */
   Book1.title = "Go Language"
   Book1.author = "www.welookups.com"
   Book1.subject = "Go Language tutorial"
   Book1.book_id = 6495407

   /* book 2 description */
   Book2.title = "Python Tutorial"
   Book2.author = "www.welookups.com"
   Book2.subject = "Python Language tutorial"
   Book2.book_id = 6495700

   /* 打印 Book1 information */
   printBook(Book1)

   /* 打印 Book2 information */
   printBook(Book2)
}

func printBook( book Books ) {
   fmt.Printf( "Book title : %s\n", book.title)
   fmt.Printf( "Book author : %s\n", book.author)
   fmt.Printf( "Book subject : %s\n", book.subject)
   fmt.Printf( "Book book_id : %d\n", book.book_id)
}

The above example execution results are:

Book title : Go Language
Book author : www.welookups.com
Book subject : Go Language tutorial
Book book_id : 6495407
Book title : Python Tutorial
Book author : www.welookups.com
Book subject : Python Language tutorial
Book book_id : 6495700

Structure pointer

You can define a pointer to a structure similar to other pointer variables, in the following format:

var struct_pointer *Books

The pointer variable defined above can store the address of the structure variable. View the structure variable address, you can & The symbol is placed before the structure variable:

struct_pointer = &Book1

Use the structure pointer to access the structure member, using the "." operator:

struct_pointer.title

Next let's rewrite the above example using the structure pointer, the code is as follows:

example

package main

import "fmt"

type Books struct {
   title string
   author string
   subject string
   book_id int
}

func main() {
   var Book1 Books        /* Declare Book1 of type Book */
   var Book2 Books        /* Declare Book2 of type Book */

   /* book 1 description */
   Book1.title = "Go Language"
   Book1.author = "www.welookups.com"
   Book1.subject = "Go Language tutorial"
   Book1.book_id = 6495407

   /* book 2 description */
   Book2.title = "Python Tutorial"
   Book2.author = "www.welookups.com"
   Book2.subject = "Python Language tutorial"
   Book2.book_id = 6495700

   /* 打印 Book1 information */
   printBook(&Book1)

   /* 打印 Book2 information */
   printBook(&Book2)
}
func printBook( book *Books ) {
   fmt.Printf( "Book title : %s\n", book.title)
   fmt.Printf( "Book author : %s\n", book.author)
   fmt.Printf( "Book subject : %s\n", book.subject)
   fmt.Printf( "Book book_id : %d\n", book.book_id)
}

The above example execution results are:

Book title : Go Language
Book author : www.welookups.com
Book subject : Go Language tutorial
Book book_id : 6495407
Book title : Python Tutorial
Book author : www.welookups.com
Book subject : Python Language tutorial
Book book_id : 6495700