从未使用此预订值 (SA4006)

我正在尝试学习 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 - userTickets

bookings = append(bookings, firstName+" "+lastName)

它说“这个预订价值从未使用过(SA4006)”


所以,这里是整个代码:


https://go.dev/play/p/GFMR7f64li1


package main


import (

    "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.")

            }


        }


    }


}



潇湘沐
浏览 123回答 2
2回答

动漫人物

因此,我对代码进行了一些清理,现在它又按预期工作了。现在作为包级别变量bookings在外部并且它可以工作。mainpackage mainimport (    "fmt"    "strings")const conferenceTickets int = 50var conferenceName = "Go Conference"var remainingTickets uint = 50var 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)}多谢你们。

收到一只叮咚

与大多数其他主流语言不同,如果你不使用变量的值做某事,Go 就不允许使用变量。因此,在测试代码中解决这个问题的方法是将值打印到控制台。在最后添加这一行:fmt.Println(bookings)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go