Most basic defer implementation

Report a typo

Daniel had a Go program that took as input the first name of a person, then the last name, and finally printed it to terminal:

James // firstName
Bond  // lastName

He wanted to change his program, for the output to be just like James Bond's famous introduction: "Bond, James Bond":

Bond,
James Bond

Daniel tried to change his Go program to achieve this type of output by implementing defer statements. However, he's made some mistakes. To fix the program and get the proper output, you need to delete some defer statements and also add a new one!

Sample Input 1:

James
Bond

Sample Output 1:

Bond,
James Bond
Write a program in Go
package main

import "fmt"

func main() {
var firstName string
var lastName string

// delete or add new defer statements below!
fmt.Scanln(&firstName)
defer fmt.Scanln(&lastName)

fmt.Printf("%s %s", firstName, lastName)
defer fmt.Printf("%s,\n", lastName)
}
___

Create a free account to access the full topic