Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Go structure as a function argument

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Go structure as a function parameter". In daily operation, I believe many people have doubts about how to use Go structure as a function parameter. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use Go structure as a function parameter". Next, please follow the editor to study!

Structure as a function parameter

You can pass the structure type as an argument to the function like any other data type. And access the structure variables as shown 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 / * declare Book1 to be of type Books * /

Var Book2 Books / * declare Book2 to be of type Books * /

/ * book 1 description * /

Book1.title = "Go language"

Book1.author = "www.runoob.com"

Book1.subject = "Go language tutorial"

Book1.book_id = 6495407

/ * book 2 description * /

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 execution result of the above example is:

Book title: Go language Book author: Book subject: Go language tutorials Book book_id: 6495407Book title: Python tutorials Book author: Book subject: Python language tutorials Book book_id: 6495700 this is the end of the study on "how to use Go structures as function parameters", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report