Home > database >  this value of bookings is never used (SA4006)
this value of bookings is never used (SA4006)

Time:01-03

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:

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:

[email protected]

Enter number of tickets:

3

Thank you caio rodrigues for booking 3 tickets. You will receive a confirmation email at [email protected]

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:

[email protected]

Enter number of tickets:

5

Thank you john smith for booking 5 tickets. You will receive a confirmation email at [email protected]

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.

CodePudding user response:

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)

CodePudding user response:

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.

  • Related