Home »
Golang »
Golang Find Output Programs
Golang Conversions | Find Output Programs | Set 1
This section contains the Golang conversions find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on October 21, 2021
Program 1:
package main
import "fmt"
import "strconv"
func main() {
var str1 string = "123"
var str2 string = "abc"
var num int64
num, _ = strconv.ParseInt(str1, 0, 64)
fmt.Println("Num : ", num)
num, _ = strconv.ParseInt(str2, 0, 64)
fmt.Println("Num : ", num)
}
Output:
Num : 123
Num : 0
Explanation:
In the main() function, we created two string variables. Then we converted the created strings into numbers using ParseInt() function. The ParseInt() returns 0 if the given string does not contain a number. After that, we printed the result.
Program 2:
package main
import "fmt"
import "strconv"
func main() {
var str1 string = "123"
var str2 string = "456"
var ptr *string
var num int64
ptr = &str1
num, _ = strconv.ParseInt(*ptr, 0, 64)
fmt.Println("Num : ", num)
ptr = &str2
num, _ = strconv.ParseInt(*ptr, 0, 64)
fmt.Println("Num : ", num)
}
Output:
Num : 123
Num : 456
Explanation:
In the main() function, we created two string variables and a pointer to the string. Then we converted the created strings into numbers with pointer using ParseInt() function. The ParseInt() returns 0 if the given string does not contain a number. After that, we printed the result.
Program 3:
package main
import "fmt"
func main() {
var str1 string = "123"
var str2 string = "456"
var num int64
num, _ = int64(str1)
fmt.Println("Num : ", num)
num, _ = int64(str2)
fmt.Println("Num : ", num)
}
Output:
./prog.go:10:9: assignment mismatch: 2 variables but 1 value
./prog.go:10:16: cannot convert str1 (type string) to type int64
./prog.go:13:9: assignment mismatch: 2 variables but 1 value
./prog.go:13:16: cannot convert str2 (type string) to type int64
Explanation:
The above program will generate syntax errors because we cannot convert the string into an integer using the int64() function.
Program 4:
package main
import "fmt"
func main() {
var num1 float64 = 123.45
var num2 float64 = 456.12
var ptr *float64
var num int64
ptr = &num1
num = int64(*ptr)
fmt.Println("Num1 : ", num)
ptr = &num2
num = int64(*ptr)
fmt.Println("Num2 : ", num)
}
Output:
Num1 : 123
Num2 : 456
Explanation:
In the main() function, we created two float variables and a pointer to float. Then we converted the created float numbers into integer numbers with pointer using int64() function. After that, we printed the result.
Program 5:
package main
import "fmt"
func main() {
var num1 float64 = 123.45
var num2 float64 = -456.12
var ptr *float64
var num int64
ptr = &num1
num = byte(*ptr)
fmt.Println("Num1 : ", num)
ptr = &num2
num = byte(*ptr)
fmt.Println("Num2 : ", num)
}
Output:
./prog.go:12:6: cannot use byte(*ptr) (type byte) as type int64 in assignment
./prog.go:16:6: cannot use byte(*ptr) (type byte) as type int64 in assignment
Explanation:
The above program will generate syntax errors. Because byte() function returns byte value after conversion but here we tried to assign byte value into an integer.
Golang Find Output Programs »