英文:
this value of bookings is never used (SA4006)
问题
我正在尝试学习Golang,并且我正在跟随Techworld with Nana在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)
错误信息显示为"this value of bookings is never used (SA4006)"。
这是完整的代码:
https://go.dev/play/p/GFMR7f64li1
当我使用输入进行测试时,请求的姓氏没有被添加。
这是终端的输出:
Welcome to Go Conference booking application!
We have a total of 50 tickets and 50 are still available.
Get your tickets here to attend.
Enter your first name:
caio
Enter your last name:
rodrigues
Enter your e-mail address:
cc@cc.com
Enter number of tickets:
3
Thank you caio rodrigues for booking 3 tickets. You will receive a confirmation email at cc@cc.com
47 tickets remaining for the Go Conference.
[caio rodrigues]
**The first name of our bookings are: []**
Enter your first name:
john
Enter your last name:
smith
Enter your e-mail address:
jj@jj.com
Enter number of tickets:
5
Thank you john smith for booking 5 tickets. You will receive a confirmation email at jj@jj.com
45 tickets remaining for the Go Conference.
[john smith]
**The first name of our bookings are: []**
它应该将姓氏添加到列表中。
英文:
I'm trying to learn Golang and as I am doing a tutorial on Youtube with Techworld with Nana, and learning to group logic with functions, it shows an error that was not there earlier, and it does not show for her on the video.
bookTicket(remainingTickets, userTickets, bookings, firstName, lastName, email, conferenceName)
Above is the call for the function below:
func bookTicket(remainingTickets uint, userTickets uint, bookings []string, firstName string, lastName string, email string, conferenceName string) {
remainingTickets = remainingTickets - userTickets
bookings = append(bookings, firstName+" "+lastName)
It says "this value of bookings is never used (SA4006)"
So, here is the whole code:
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.")
}
}
}
}
func greetUsers(confName string, confTickets int, remainingTickets uint) {
fmt.Printf("Welcome to %v booking application!\n", confName)
fmt.Printf("We have a total of %v tickets and %v are still available.\n", confTickets, remainingTickets)
fmt.Println("Get your tickets here to attend.")
}
func getFirstNames(bookings []string) []string {
firstNames := []string{}
for _, booking := range bookings {
var names = strings.Fields(booking)
firstNames = append(firstNames, names[0])
}
return firstNames
}
func validateUserInput(firstName string, lastName string, email string, userTickets uint, remainingTickets uint) (bool, bool, bool) {
isValidName := len(firstName) >= 2 && len(lastName) >= 2
isValidEmail := strings.Contains(email, "@")
isValidTicketNumber := userTickets > 0 && userTickets <=
remainingTickets
return isValidName, isValidEmail, isValidTicketNumber
}
func getUserInput() (string, string, string, uint) {
var firstName string
var lastName string
var email string
var userTickets uint
fmt.Println("Enter your first name: ")
fmt.Scan(&firstName)
fmt.Println("Enter your last name: ")
fmt.Scan(&lastName)
fmt.Println("Enter your e-mail address: ")
fmt.Scan(&email)
fmt.Println("Enter number of tickets: ")
fmt.Scan(&userTickets)
return firstName, lastName, email, userTickets
}
func bookTicket(remainingTickets uint, userTickets uint, bookings []string, firstName string, lastName string, email string,
conferenceName 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)
fmt.Println(bookings)
}
When I test it out with inputs, the first name isn't appended like requested.
This is the tutorial I'm following.
And this is the terminal:
Welcome to Go Conference booking application!
We have a total of 50 tickets and 50 are still available.
Get your tickets here to attend.
Enter your first name:
caio
Enter your last name:
rodrigues
Enter your e-mail address:
cc@cc.com
Enter number of tickets:
3
Thank you caio rodrigues for booking 3 tickets. You will receive a
confirmation email at cc@cc.com
47 tickets remaining for the Go Conference.
[caio rodrigues]
**The first name of our bookings are: []**
Enter your first name:
john
Enter your last name:
smith
Enter your e-mail address:
jj@jj.com
Enter number of tickets:
5
Thank you john smith for booking 5 tickets. You will receive a
confirmation email at jj@jj.com
45 tickets remaining for the Go Conference.
[john smith]
**The first name of our bookings are: []**
It was supposed to append the first name to the list.
答案1
得分: 0
与大多数其他主流语言不同,Go语言不允许在不使用变量值的情况下使用变量。因此,在测试代码中解决这个问题的方法是将变量的值打印到控制台。在代码的末尾添加以下行:
fmt.Println(bookings)
英文:
Unlike most other mainstream languages, Go does not allow the use of variables if you don't do something with their value. So, the way way to get around this in test code is to print the value to console. Add this line at the end:
fmt.Println(bookings)
答案2
得分: 0
所以,我对代码进行了一些清理,现在它又按预期工作了。
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)
}
谢谢,伙计们。
英文:
So, I did some cleaning up on the code and now it's working again as intended.
The bookings
is now outside of the main
as a package level variable and it works.
package main
import (
"fmt"
"strings"
)
const conferenceTickets int = 50
var conferenceName = "Go Conference"
var remainingTickets uint = 50
var bookings = []string{}
And the bookTickets
call is now:
bookTicket(userTickets, firstName, lastName, email)
calling the function:
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)
}
Thanks, guys.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论