用go语言编写程序定义一个Books结构体,包含成员title、author、subject和book_id,在main()函数中初始化Books结构体对象并打印。
时间: 2024-02-12 13:02:52 浏览: 41
下面是一个简单的例子:
```go
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
book := Books{
title: "Golang Programming",
author: "John Smith",
subject: "Programming Language",
book_id: 12345,
}
fmt.Println(book)
}
```
在上面的示例中,我们定义了一个名为 `Books` 的结构体,包含了 `title`、`author`、`subject` 和 `book_id` 四个成员变量。然后在 `main` 函数中创建了一个 `book` 变量,并使用结构体字面量初始化了它的成员变量。最后,使用 `fmt.Println` 函数输出了这个 `book` 变量。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)