2 回答
TA贡献1815条经验 获得超10个赞
因此,我对代码进行了一些清理,现在它又按预期工作了。
现在作为包级别变量bookings在外部并且它可以工作。main
package main
import (
"fmt"
"strings"
)
const conferenceTickets int = 50
var conferenceName = "Go Conference"
var remainingTickets uint = 50
var bookings = []string{}
现在的bookTickets电话是:
bookTicket(userTickets, firstName, lastName, email)
调用函数:
func bookTicket(userTickets uint, firstName string, lastName string, email string) {
remainingTickets = remainingTickets - userTickets
bookings = append(bookings, firstName+" "+lastName)
fmt.Printf("Thank you %v %v for booking %v tickets. You will receive a confirmation email at %v\n", firstName, lastName, userTickets, email)
fmt.Printf("%v tickets remaining for the %v.\n", remainingTickets, conferenceName)
}
多谢你们。
TA贡献1821条经验 获得超4个赞
与大多数其他主流语言不同,如果你不使用变量的值做某事,Go 就不允许使用变量。因此,在测试代码中解决这个问题的方法是将值打印到控制台。在最后添加这一行:
fmt.Println(bookings)
- 2 回答
- 0 关注
- 122 浏览
添加回答
举报