2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.

2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.

package mainimport ("fmt")func main() {num := 65str := string(num)fmt.Printf("%v, %T\n", str, str)}

Answer 2022-08-05:

This question can be either A or D.But the correct answer is B.However, if you check with go vet, it will prompt: conversion from int to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?).
In other words, if you want to convert the number 65 into a string, you cannot use string(num). If you convert it in this way, you will get a string representation of rune, because the ASCII code of literal A is 65,So the output here is A,string.

insert image description here


thank
Similar articles