Data type conversion, string to int

67 阅读1分钟

Data type conversion, string to int

I would like to record the things I have learned, in English. To record knowledge and practise my English. Keep collecting streams, hoping one day to become rivers and seas.

For whom are computer science majors or experenced, these thing isn't a big deal. Actually it is the very basic thing. For me who is a begineer of computer science and learning coding by myself, the data type conversion likes a magic.

image.png This screen is from the package stconv of Golang. It shows the function to convert string to int. It isn't a magic but a calculation with the ASCII code.

n = n*10 + int(ch)

With this formula we will get the int number.

_, ch := range []byte(s)

With this program statement we will get the ASCII code for every character in the string s. How does it do that? I believe the Go runtime starts a system call and the OS returns the ASCII code of the characters.