为了账号安全,请及时绑定邮箱和手机立即绑定

从未使用此预订值 (SA4006)

从未使用此预订值 (SA4006)

Go
潇湘沐 2022-10-24 09:56:37
我正在尝试学习 Golang,因为我正在与 Nana 一起使用 Techworld 在 Youtube 上做一个教程,并学习使用函数对逻辑进行分组,它显示了一个之前不存在的错误,并且它没有在视频中显示给她。bookTicket(remainingTickets, userTickets, bookings, firstName, lastName, email, conferenceName)以上是对以下函数的调用:func bookTicket(remainingTickets uint, userTickets uint, bookings []string, firstName string, lastName string, email string, conferenceName string) {remainingTickets = remainingTickets - userTicketsbookings = append(bookings, firstName+" "+lastName)它说“这个预订价值从未使用过(SA4006)”所以,这里是整个代码:https://go.dev/play/p/GFMR7f64li1package mainimport (    "fmt"    "strings")func main() {    conferenceName := "Go Conference"    const conferenceTickets int = 50    var remainingTickets uint = 50    bookings := []string{}    greetUsers(conferenceName, conferenceTickets, remainingTickets)    for {        firstName, lastName, email, userTickets := getUserInput()        isValidName, isValidEmail, isValidTicketNumber := validateUserInput(firstName, lastName, email, userTickets, remainingTickets)        if isValidName && isValidEmail && isValidTicketNumber {            bookTicket(remainingTickets, userTickets, bookings, firstName, lastName, email, conferenceName)            firstNames := getFirstNames(bookings)            fmt.Printf("The first name of our bookings are: %v\n", firstNames)            if remainingTickets == 0 {                // sair do loop/programa                fmt.Println("Our conference is booked out. Come back next year.")                break            }        } else {            if !isValidName {                fmt.Println("First name or last name too short.")            }            if !isValidEmail {                fmt.Println("E-mail doesn't contain @ sign.")            }            if !isValidTicketNumber {                fmt.Println("Number of tickets entered is invalid.")            }        }    }}
查看完整描述

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)

}

多谢你们。


查看完整回答
反对 回复 2022-10-24
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

与大多数其他主流语言不同,如果你不使用变量的值做某事,Go 就不允许使用变量。因此,在测试代码中解决这个问题的方法是将值打印到控制台。在最后添加这一行:

fmt.Println(bookings)


查看完整回答
反对 回复 2022-10-24
  • 2 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信