PDO(PHP Data Objects)란 여러가지 데이터베이스를 제어하는 방법을 표준화시킨 것이다. 데이터베이스는 다양한 종류가 있다. 그리고 종류에 따라서 서로 다른 드라이브를 사용해 왔는데 드라이브의 종류에 따라서 데이터베이스를 제어하기 위한 API가 달랐다. PDO를 사용하면 동일한 방법으로 데이터베이스를 제어할 수 있다.
PDO를 사용하기 위해서는
1
2
3
4
5
6
7
8
9
10
## 해당 파일에서vi /etc/php/7.4/fpm/php.ini
## 아래 pdo_mysql 주석을 제거 한다.;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
mysql 사전 작업
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## 데이터베이스(스키마) 생성CREATE DATABASE opentutorials CHARACTER SET utf8 COLLATE utf8_general_ci;## 설정 할 스키마 선택use opentutorials;## TABLE 생성CREATE TABLE topic ( id int(11) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL ,
description text NULL ,
created datetime NOT NULL ,
PRIMARY KEY (id));
<?php$dbh=newPDO('mysql:host=localhost;dbname=opentutorials','root','111111',array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8"));switch($_GET['mode']){case'insert':$stmt=$dbh->prepare("INSERT INTO topic (title, description, created) VALUES (:title, :description, now())");$stmt->bindParam(':title',$title);$stmt->bindParam(':description',$description);$title=$_POST['title'];$description=$_POST['description'];$stmt->execute();header("Location: list.php");break;case'delete':$stmt=$dbh->prepare('DELETE FROM topic WHERE id = :id');$stmt->bindParam(':id',$id);$id=$_POST['id'];$stmt->execute();header("Location: list.php");break;case'modify':$stmt=$dbh->prepare('UPDATE topic SET title = :title, description = :description WHERE id = :id');$stmt->bindParam(':title',$title);$stmt->bindParam(':description',$description);$stmt->bindParam(':id',$id);$title=$_POST['title'];$description=$_POST['description'];$id=$_POST['id'];$stmt->execute();header("Location: list.php?id={$_POST['id']}");break;}?>
packagemainimport"fmt"funcmain(){varconferenceName="Go Conference"constconferenceTickets=50fmt.Println("Welcome to",conferenceName,"booking application")fmt.Println("Get your tickets here to attend")conferenceTickets=30fmt.Println(conferenceTickets)}
const로 정의를 하면 아래처럼 값을 변경 할 수 없다.
var로 변경을 하면 아래에서 30으로 변경이 가능하다.
2.3. printf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
packagemainimport"fmt"funcmain(){varconferenceName="Go Conference"constconferenceTickets=50varremainingTickets=50fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}
packagemainimport"fmt"funcmain(){conferenceName:="Go Conference"constconferenceTickets=50varremainingTickets=50fmt.Printf("conferenceTickets is %T, remainingTickets is %T, conferencename is %T\n",conferenceTickets,remainingTickets,conferenceName)fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")varuserNamestringvaruserTicketsintuserName="TOM"userTickets=2fmt.Printf("User %v booked %v tickets.\n",userName,userTickets)}
%T로 했을 경우 DataType을 확인 할 수 있다.
2.4. INPUT, 포인터를 사용해야한다.
& <- 포인터를 사용
fmt.Scan(&useraName) 을하면 사용자가 입력을 할 수 있게 해준다.
포인터를 사용하지 않을 경우 저장된 메모리 값을 확인 할 수 있다.
packagemainimport"fmt"funcmain(){conferenceName:="Go Conference"constconferenceTickets=50varremainingTicketsuint=50fmt.Printf("conferenceTickets is %T, remainingTickets is %T, conferencename is %T\n",conferenceTickets,remainingTickets,conferenceName)fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)// 이지점에서 사용자에게 물어봄
fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)// 이지점에서 사용자에게 물어봄
fmt.Println("Enter Your email: ")fmt.Scan(&email)// 이지점에서 사용자에게 물어봄
fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)// 이지점에서 사용자에게 물어봄
remainingTickets=remainingTickets-userTicketsfmt.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 %v\n",remainingTickets,conferenceName)}
2.5. Array
배열의 경우 공간중간 값이 없어도 빈공간으로 인식을 하기 때문에 메모리를 그만큼 할당 하게 된다.
packagemainimport"fmt"funcmain(){conferenceName:="Go Conference"constconferenceTickets=50varremainingTicketsuint=50varbookings[50]stringfmt.Printf("conferenceTickets is %T, remainingTickets is %T, conferencename is %T\n",conferenceTickets,remainingTickets,conferenceName)fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)remainingTickets=remainingTickets-userTicketsbookings[0]=firstName+" "+lastNamefmt.Printf("The whole array: %v\n",bookings)fmt.Printf("The first value: %v\n",bookings[0])fmt.Printf("The first value: %T\n",bookings)fmt.Printf("The first value: %v\n",len(bookings))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 %v\n",remainingTickets,conferenceName)}
공간이 이미 할당이 되어 있는 것을 확인 할 수 있다.
2.6. Slice
Slice구성 아래 2가지가 변경되고 하나 추가됨.
var bookings [50]string -> bookings := []string{}
packagemainimport"fmt"funcmain(){conferenceName:="Go Conference"constconferenceTickets=50varremainingTicketsuint=50bookings:=[]string{}fmt.Printf("conferenceTickets is %T, remainingTickets is %T, conferencename is %T\n",conferenceTickets,remainingTickets,conferenceName)fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)remainingTickets=remainingTickets-userTicketsbookings=append(bookings,firstName+" "+lastName)fmt.Printf("The whole slice: %v\n",bookings)fmt.Printf("The first value: %v\n",bookings[0])fmt.Printf("The first type: %T\n",bookings)fmt.Printf("The first lgenth: %v\n",len(bookings))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 %v\n",remainingTickets,conferenceName)fmt.Printf("These are all our booking: %v\n",bookings)}
packagemainimport("fmt""strings")funcmain(){conferenceName:="Go Conference"constconferenceTicketsint=50varremainingTicketsuint=50bookings:=[]string{}fmt.Printf("conferenceTickets is %T, remainingTickets is %T, conferencename is %T\n",conferenceTickets,remainingTickets,conferenceName)fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")for{varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)isValidName:=len(firstName)>=2&&len(lastName)>=2isValidEmail:=strings.Contains(email,"@")isValidTicketNumber:=userTickets>0&&userTickets<=remainingTicketsifisValidName&&isValidEmail&&isValidTicketNumber{remainingTickets=remainingTickets-userTicketsbookings=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 %v\n",remainingTickets,conferenceName)firstNames:=[]string{}for_,booking:=rangebookings{varnames=strings.Fields(booking)firstNames=append(firstNames,names[0])}fmt.Printf("These first names of bookings are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}
packagemainimport("fmt""strings")funcmain(){conferenceName:="Go Conference"constconferenceTicketsint=50varremainingTicketsuint=50bookings:=[]string{}greetUsers(conferenceName,conferenceTickets,remainingTickets)for{varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)isValidName:=len(firstName)>=2&&len(lastName)>=2isValidEmail:=strings.Contains(email,"@")isValidTicketNumber:=userTickets>0&&userTickets<=remainingTicketsifisValidName&&isValidEmail&&isValidTicketNumber{remainingTickets=remainingTickets-userTicketsbookings=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 %v\n",remainingTickets,conferenceName)printFirstNames(bookings)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(confNamestring,confTicketsint,remainingTicketsuint){fmt.Printf("Welcome to %v booking application\n",confName)fmt.Printf("We have total of %v tickets and %v are still available\n",confTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcprintFirstNames(bookings[]string){firstNames:=[]string{}for_,booking:=rangebookings{varnames=strings.Fields(booking)firstNames=append(firstNames,names[0])}fmt.Printf("These first names of bookings are: %v\n",firstNames)}
packagemainimport("fmt""strings")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=[]string{}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{varnames=strings.Fields(booking)firstNames=append(firstNames,names[0])}returnfirstNames}funcvalidateUserInput(firstNamestring,lastNamestring,emailstring,userTicketsuint)(bool,bool,bool){isValidName:=len(firstName)>=2&&len(lastName)>=2isValidEmail:=strings.Contains(email,"@")isValidTicketNumber:=userTickets>0&&userTickets<=remainingTicketsreturnisValidName,isValidEmail,isValidTicketNumber}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsbookings=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 %v\n",remainingTickets,conferenceName)}
packagemainimport("fmt""strings")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=[]string{}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{varnames=strings.Fields(booking)firstNames=append(firstNames,names[0])}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsbookings=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 %v\n",remainingTickets,conferenceName)}
packagemainimport("GOLANG/helper""fmt""strings")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=[]string{}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=helper.ValidateUserInput(firstName,lastName,email,userTickets,remainingTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{varnames=strings.Fields(booking)firstNames=append(firstNames,names[0])}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsbookings=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 %v\n",remainingTickets,conferenceName)}
packagemainimport("fmt""strconv")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=make([]map[string]string,0)funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{firstNames=append(firstNames,booking["firtName"])}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsvaruserData=make(map[string]string)userData["firstName"]=firstNameuserData["lastName"]=lastNameuserData["email"]=emailuserData["numberOfTickets"]=strconv.FormatUint(uint64(userTickets),10)bookings=append(bookings,userData)fmt.Printf("List of bookings is %v\n",bookings)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 %v\n",remainingTickets,conferenceName)}
packagemainimport("fmt")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=make([]UserData,0)typeUserDatastruct{firstNamestringlastNamestringemailstringnumberOfTicketsuint}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{firstNames=append(firstNames,booking.firstName)}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsvaruserData=UserData{firstName:firstName,lastName:lastName,email:email,numberOfTickets:userTickets,}bookings=append(bookings,userData)fmt.Printf("List of bookings is %v\n",bookings)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 %v\n",remainingTickets,conferenceName)}
packagemainimport("fmt""time")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=make([]UserData,0)typeUserDatastruct{firstNamestringlastNamestringemailstringnumberOfTicketsuint}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)sendTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{firstNames=append(firstNames,booking.firstName)}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsvaruserData=UserData{firstName:firstName,lastName:lastName,email:email,numberOfTickets:userTickets,}bookings=append(bookings,userData)fmt.Printf("List of bookings is %v\n",bookings)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 %v\n",remainingTickets,conferenceName)}funcsendTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){time.Sleep(10*time.Second)varticket=fmt.Sprintf("%v tickets for %v %v",userTickets,firstName,lastName)fmt.Println("#########################")fmt.Printf("Sending ticket: \n %v to email address %v\n",ticket,email)fmt.Println("#########################")}
packagemainimport("fmt""time")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=make([]UserData,0)typeUserDatastruct{firstNamestringlastNamestringemailstringnumberOfTicketsuint}funcmain(){greetUsers()for{firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)// go라는 것을 넣어주면 된다.
gosendTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")break}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}}}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{firstNames=append(firstNames,booking.firstName)}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsvaruserData=UserData{firstName:firstName,lastName:lastName,email:email,numberOfTickets:userTickets,}bookings=append(bookings,userData)fmt.Printf("List of bookings is %v\n",bookings)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 %v\n",remainingTickets,conferenceName)}funcsendTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){time.Sleep(10*time.Second)varticket=fmt.Sprintf("%v tickets for %v %v",userTickets,firstName,lastName)fmt.Println("#########################")fmt.Printf("Sending ticket: \n %v to email address %v\n",ticket,email)fmt.Println("#########################")}
packagemainimport("fmt""sync""time")constconferenceTicketsint=50varconferenceNamestring="Go Conference"varremainingTicketsuint=50varbookings=make([]UserData,0)typeUserDatastruct{firstNamestringlastNamestringemailstringnumberOfTicketsuint}varwg=sync.WaitGroup{}funcmain(){greetUsers()//for {
firstName,lastName,email,userTickets:=getUserInput()isValidName,isValidEmail,isValidTicketNumber:=validateUserInput(firstName,lastName,email,userTickets)ifisValidName&&isValidEmail&&isValidTicketNumber{bookTicket(userTickets,firstName,lastName,email)wg.Add(1)gosendTicket(userTickets,firstName,lastName,email)firstNames:=getFirstNames()fmt.Printf("The first names of bookins are: %v\n",firstNames)ifremainingTickets==0{fmt.Println("Our conference is booked out. Come back next year.")//break
}}else{if!isValidName{fmt.Println("first name or last name you entered is too short")}if!isValidEmail{fmt.Println("email address you entered doesn't contain @ sign")}if!isValidTicketNumber{fmt.Println("number of tickets you entered is invalid")}}wg.Wait()//}
}funcgreetUsers(){fmt.Printf("Welcome to %v booking application\n",conferenceName)fmt.Printf("We have total of %v tickets and %v are still available\n",conferenceTickets,remainingTickets)fmt.Println("Get your tickets here to attend")}funcgetFirstNames()[]string{firstNames:=[]string{}for_,booking:=rangebookings{firstNames=append(firstNames,booking.firstName)}returnfirstNames}funcgetUserInput()(string,string,string,uint){varfirstNamestringvarlastNamestringvaremailstringvaruserTicketsuintfmt.Println("Enter Your firstName: ")fmt.Scan(&firstName)fmt.Println("Enter Your lastName: ")fmt.Scan(&lastName)fmt.Println("Enter Your email: ")fmt.Scan(&email)fmt.Println("Enter number of tickets: ")fmt.Scan(&userTickets)returnfirstName,lastName,email,userTickets}funcbookTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){remainingTickets=remainingTickets-userTicketsvaruserData=UserData{firstName:firstName,lastName:lastName,email:email,numberOfTickets:userTickets,}bookings=append(bookings,userData)fmt.Printf("List of bookings is %v\n",bookings)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 %v\n",remainingTickets,conferenceName)}funcsendTicket(userTicketsuint,firstNamestring,lastNamestring,emailstring){time.Sleep(10*time.Second)varticket=fmt.Sprintf("%v tickets for %v %v",userTickets,firstName,lastName)fmt.Println("#########################")fmt.Printf("Sending ticket: \n %v to email address %v\n",ticket,email)fmt.Println("#########################")wg.Done()}