Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Structs in Go

Structs in Go

Arrays in Go can store data of the same type, but in a struct we can define different data types for different items.

A struct is a data collection composed of a series of data items of the same or different types.

A struct represents a record, such as a library book record, where each book has the following attributes:


Defining Structs

A struct definition requires the type and struct statements. The struct statement defines a new data type with one or more members, and the type statement sets the name of the struct. The format of a struct is as follows:

type struct_variable_type struct {

   member definition

   member definition

   ...

   member definition

}

Once a struct type is defined, it can be used in variable declarations; the syntax is as follows:



variable_name := structure_variable_type {value1, value2...valuen}

或

variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}

The 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 struct

    fmt

.

Println

(

Books

{

"Go language"

,

"www.runoob.com"

,

"Go language tutorial"

,

6495407

})

   

// you can also use the key => value format

    fmt

.

Println

(

Books

{

title

:

"Go language"

,

author

:

"www.runoob.com"

,

subject

:

"Go language tutorial"

,

book_id

:

6495407

})

   

// omitted fields are 0 or empty

   fmt

.

Println

(

Books

{

title

:

"Go language"

,

author

:

"www.runoob.com"

})

}

The output is:

{Go 语言 www.runoob.com Go 语言教程 6495407}

{Go 语言 www.runoob.com Go 语言教程 6495407}

{Go 语言 www.runoob.com  0}


Accessing struct members

To access struct members, you need to use the dot . operator; the format is:

结构体.成员名"

A variable of struct type is defined using the struct keyword; example 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 as type Books */

   

var

Book2 Books        

/* declare Book2 as type Books */

   

/* description of book 1 */

   Book1

.

title

=

"Go language"

   Book1

.

author

=

"www.runoob.com"

   Book1

.

subject

=

"Go language tutorial"

   Book1

.

book_id

=

6495407

   

/* description of book 2 */

   Book2

.

title

=

"Python Tutorial"

   Book2

.

author

=

"www.runoob.com"

   Book2

.

subject

=

"Python language tutorial"

   Book2

.

book_id

=

6495700

   

/* print 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

)

   

/* print 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 result of running the above example is:

Book 1 title : Go 语言

Book 1 author : www.runoob.com

Book 1 subject : Go 语言教程

Book 1 book_id : 6495407

Book 2 title : Python 教程

Book 2 author : www.runoob.com

Book 2 subject : Python 语言教程

Book 2 book_id : 6495700


Structs as function parameters

You can pass a struct type as a parameter to a function just like other data types, and access the struct variable in the way shown in the example above:

Example

package

main

import

"fmt"

type

Books

struct

{

   title

string

   author

string

   subject

string

   book_id

int

}

func

main

()

{

   

var

Book1 Books        

/* declare Book1 as type Books */

   

var

Book2 Books        

/* declare Book2 as type Books */

   

/* description of book 1 */

   Book1

.

title

=

"Go language"

   Book1

.

author

=

"www.runoob.com"

   Book1

.

subject

=

"Go language tutorial"

   Book1

.

book_id

=

6495407

   

/* description of book 2 */

   Book2

.

title

=

"Python Tutorial"

   Book2

.

author

=

"www.runoob.com"

   Book2

.

subject

=

"Python language tutorial"

   Book2

.

book_id

=

6495700

   

/* print Book1 information */

   printBook

(

Book1

)

   

/* print 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 result of running the above example is:

Book title : Go 语言

Book author : www.runoob.com

Book subject : Go 语言教程

Book book_id : 6495407

Book title : Python 教程

Book author : www.runoob.com

Book subject : Python 语言教程

Book book_id : 6495700


Pointers to structs

You can define a pointer to a struct similar to other pointer variables; the format is as follows:

var struct_pointer *Books

The pointer variable defined above can store the address of a struct variable. To view the address of a struct variable, place the & sign before the struct variable:

struct_pointer = &Book1

To access struct members through a struct pointer, use the "." operator:

struct_pointer.title

Next, let's rewrite the above example using a struct 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 as type Books */

   

var

Book2 Books        

/* declare Book2 as type Books */

   

/* description of book 1 */

   Book1

.

title

=

"Go language"

   Book1

.

author

=

"www.runoob.com"

   Book1

.

subject

=

"Go language tutorial"

   Book1

.

book_id

=

6495407

   

/* description of book 2 */

   Book2

.

title

=

"Python Tutorial"

   Book2

.

author

=

"www.runoob.com"

   Book2

.

subject

=

"Python language tutorial"

   Book2

.

book_id

=

6495700

   

/* print Book1 information */

   printBook

(

&Book1

)

   

/* print 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 result of running the above example is:

Book title : Go 语言

Book author : www.runoob.com

Book subject : Go 语言教程

Book book_id : 6495407

Book title : Python 教程

Book author : www.runoob.com

Book subject : Python 语言教程

Book book_id : 6495700