0% found this document useful (0 votes)
54 views4 pages

Go Packages

- Go programs are organized into packages, with a package being a collection of Go files - In the example code, a helper.go file is created in the main package to define a validateUserInput function - To use the function from another file, the helper package must be imported and the function needs to be exported by capitalizing the first letter

Uploaded by

Bahadur Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Go Packages

- Go programs are organized into packages, with a package being a collection of Go files - In the example code, a helper.go file is created in the main package to define a validateUserInput function - To use the function from another file, the helper package must be imported and the function needs to be exported by capitalizing the first letter

Uploaded by

Bahadur Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Go Packages

Go programs are organized into packages

A package is a collection of Go files

As an example in our code, we created a file called helper.go in the same main package

helper.go code

package main

import "strings"

func validateUserInput(firstName string, lastName string, email string, userTickets uint) (bool, bool, bool) {
isValidName := len(firstName) >= 2 && len(lastName) >= 2 //true assigned to variable if both the conditions are true
isValidEmail := strings.Contains(email, "@") //if email contains @, it returns true
isValidTicketNumber := userTickets > 0 && userTickets <= remainingTickets

return isValidName, isValidEmail, isValidTicketNumber


}

main.go code

package main

import (
"fmt"
"strings"
)

var conferenceName = "Go Conference"


const conferenceTickets = 50
var remainingTickets uint = 50
var bookings = []string{}

func main() {

greetUsers()

for {

firstName, lastName, email, userTickets := getUserInput()

isValidName, isValidEmail, isValidTicketNumber := validateUserInput(firstName, lastName, email, userTickets)

if isValidName && isValidEmail && isValidTicketNumber {

bookTicket(userTickets, firstName, lastName, email)

//call function print first names


printFirstNames()

if remainingTickets == 0 {

fmt.Println("Our conference is booked out. Come back next year.")


break
}
} else {
if !isValidName{
fmt.Println("first name or last name is too short")
}
if !isValidEmail{

Go Packages 1
fmt.Println("email does not contain @")
}
if !isValidTicketNumber{
fmt.Println("number of ticket is invalid")
}
}

func greetUsers() {
fmt.Printf("Welcome to %v booking application\n", conferenceName)
fmt.Printf("We have total of %v tickets and %v tickets are left.\n", conferenceTickets, remainingTickets)
fmt.Println("Get your tickets here to attend")
}

func printFirstNames(){
firstNames := []string{}
for _, booking := range bookings {
var names = strings.Fields(booking)
firstNames = append(firstNames, names[0])
}

fmt.Printf("The first names of bookings are: %v\n", firstNames)


}

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 email: ")


fmt.Scan(&email)

fmt.Println("Enter number of tickets: ")


fmt.Scan(&userTickets)

return firstName, lastName, email, userTickets


}

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, emai
fmt.Printf("%v tickets remaining for %v\n", remainingTickets, conferenceName)
}

Now to run the program, the command is not same, use different command.

$ go run .
This command will run all the files in the current folder.

Multiple Packages in application

Go Packages 2
To make a different package, we create a folder for that package. As an example in our code for helper.
Now in the main.go, the validateUserInput() function is not defined because now it does not belong to the same package.

Import the package in main and export the function from other file.

//Import
import (
"go-booking-app/helper" //go-booking-app is the main module name defined in go.mod
)

//export

To export a function just capitalize the first letter of the function.

Go Packages 3
Go Packages 4

You might also like