Home »
Golang »
Golang Find Output Programs
Golang Goroutine, Map, and Reflection | Find Output Programs | Set 3
This section contains the Golang Goroutine, Map, and Reflection find output programs (set 3) with their output and explanations.
Submitted by Nidhi, on November 09, 2021
Program 1:
package main
import "fmt"
func main() {
CountryCode := make(map[int]string)
CountryCode["ind"] = 101
CountryCode["aus"] = 102
CountryCode["eng"] = 103
CountryCode["pak"] = 104
CountryCode["usa"] = 105
fmt.Println("India :", CountryCode["ind"])
fmt.Println("Australia:", CountryCode["aus"])
fmt.Println("England :", CountryCode["eng"])
fmt.Println("Pakistan :", CountryCode["pak"])
fmt.Println("USA :", CountryCode["usa"])
}
Output:
./prog.go:8:13: cannot use "ind" (type untyped string) as type int in map index
./prog.go:8:21: cannot use 101 (type untyped int) as type string in assignment
./prog.go:9:13: cannot use "aus" (type untyped string) as type int in map index
./prog.go:9:21: cannot use 102 (type untyped int) as type string in assignment
./prog.go:10:13: cannot use "eng" (type untyped string) as type int in map index
./prog.go:10:21: cannot use 103 (type untyped int) as type string in assignment
./prog.go:11:13: cannot use "pak" (type untyped string) as type int in map index
./prog.go:11:21: cannot use 104 (type untyped int) as type string in assignment
./prog.go:12:13: cannot use "usa" (type untyped string) as type int in map index
./prog.go:12:21: cannot use 105 (type untyped int) as type string in assignment
./prog.go:12:21: too many errors
Explanation:
The above program will generate syntax errors because we created the map with integer keys but we used string keys. We need to change on below line in the program,
CountryCode := make(map[string]int)
Program 2:
package main
import "fmt"
func main() {
CountryCode := make(map[string]int)
CountryCode.add("ind", 101)
CountryCode.add("aus", 102)
CountryCode.add("eng", 103)
CountryCode.add("pak", 104)
CountryCode.add("usa", 105)
fmt.Println("India :", CountryCode["ind"])
fmt.Println("Australia:", CountryCode["aus"])
fmt.Println("England :", CountryCode["eng"])
fmt.Println("Pakistan :", CountryCode["pak"])
fmt.Println("USA :", CountryCode["usa"])
}
Output:
./prog.go:8:13: CountryCode.add undefined (type map[string]int has no field or method add)
./prog.go:9:13: CountryCode.add undefined (type map[string]int has no field or method add)
./prog.go:10:13: CountryCode.add undefined (type map[string]int has no field or method add)
./prog.go:11:13: CountryCode.add undefined (type map[string]int has no field or method add)
./prog.go:12:13: CountryCode.add undefined (type map[string]int has no field or method add)
Explanation:
The above program will generate syntax errors because add() function is not available in this context.
Program 3:
package main
import "fmt"
func main() {
CountryCode := make(map[string]int)
CountryCode.insert("ind", 101)
CountryCode.insert("aus", 102)
CountryCode.insert("eng", 103)
CountryCode.insert("pak", 104)
CountryCode.insert("usa", 105)
fmt.Println("India :", CountryCode["ind"])
fmt.Println("Australia:", CountryCode["aus"])
fmt.Println("England :", CountryCode["eng"])
fmt.Println("Pakistan :", CountryCode["pak"])
fmt.Println("USA :", CountryCode["usa"])
}
Output:
./prog.go:8:13: CountryCode.insert undefined (type map[string]int has no field or method insert)
./prog.go:9:13: CountryCode.insert undefined (type map[string]int has no field or method insert)
./prog.go:10:13: CountryCode.insert undefined (type map[string]int has no field or method insert)
./prog.go:11:13: CountryCode.insert undefined (type map[string]int has no field or method insert)
./prog.go:12:13: CountryCode.insert undefined (type map[string]int has no field or method insert)
Explanation:
The above program will generate syntax errors because the insert() function is not available in this context.
Program 4:
package main
import "fmt"
func main() {
CountryCode := make(map[string]int)
CountryCode["ind"] = 101
CountryCode["aus"] = 102
CountryCode["eng"] = 103
CountryCode["pak"] = 104
CountryCode["usa"] = 105
delete(CountryCode, "pak")
fmt.Println("India :", CountryCode["ind"])
fmt.Println("Australia:", CountryCode["aus"])
fmt.Println("England :", CountryCode["eng"])
fmt.Println("Pakistan :", CountryCode["pak"])
fmt.Println("USA :", CountryCode["usa"])
}
Output:
India : 101
Australia: 102
England : 103
Pakistan : 0
USA : 105
Explanation:
In the above program, we created a map to store country codes. Then we added some country codes into the created map. After that, we removed an element from the map using the delete() function and printed the map elements.
Program 5:
package main
import "fmt"
func main() {
CountryCode := make(map[string]int)
CountryCode["ind"] = 101
CountryCode["aus"] = 102
CountryCode["eng"] = 103
CountryCode["pak"] = 104
CountryCode["usa"] = 105
var CloneMap = CountryCode.clone()
fmt.Println("CountryCode: ", CountryCode)
fmt.Println("CloneMap : ", CloneMap)
}
Output:
./prog.go:14:28: CountryCode.clone undefined (type map[string]int has no field or method clone)
Explanation:
The above program will generate a syntax error because the clone() function is unavailable in this context.
The correct statement is given below,
var CloneMap = CountryCode
Golang Find Output Programs »