This is a creation in Article, where the information may have evolved or changed.
Golang is a strongly typed language, which is used in the basic type conversion process. Here's a look at the common type conversions, which are constantly updated.
Shaping A string
Fmt. Println (StrConv. Itoa (100))
The source code for this method is:
Itoa is shorthand for formatint (I, ten). Func Itoa (i int) string {return Formatint (Int64 (i), 10)}
Can be seen is a simple implementation of the Formatint method.
String to reshape
I, _: = StrConv. Atoi ("+") fmt. Println (i)
64-bit shaping of the turn string
var i int64i = 0x100fmt. Println (StrConv. Formatint (i, 10))
Formatint The second parameter represents the binary, and 10 represents the decimal.
BYTE-to-32-bit shaping
B: = []byte{0x00, 0x00, 0x03, 0xe8}bytesbuffer: = bytes. Newbuffer (b) var x int32binary. Read (Bytesbuffer, Binary. Bigendian, &x) fmt. PRINTLN (x)
where binary. The Bigendian represents the byte order, and the corresponding little endian. Popular parlance is called big-endian, small end.
32-bit shaping of the turn byte
var x int32x = 106bytesBuffer: = bytes. Newbuffer ([]byte{}) binary. Write (Bytesbuffer, Binary. Bigendian, X) fmt. Println (Bytesbuffer.bytes ())
byte-to-string
Fmt. Println (String ([]byte{97, 98, 99, 100})
String to Byte
Fmt. Println ([]byte ("ABCD"))
Reprint Please specify: Happy programming»golang type conversion
Arguments and anonymous usage:
/** * Created by Administrator on 13-12-18. */package mainimport ("FMT" "OS") Func F1 (args ... interface {}) {F2 (args ...) F2 (args[1:] ...)} Func F2 (Args ... interface {}) {for I, V: = Range args {fmt. fprintf (OS. Stdout, "i =%d%v\n", I, v)}fmt. fprintf (OS. Stdout, "--------------\ n")}func main () {F1 (1, "Hello", 3.14, Main)//anonymous function 1f: = Func (i, J int) (result int) {//F is function address R Esult = i+jreturn}fmt. fprintf (OS. Stdout, "f =%v f (1,3) =%v\n", F, f (1, 3))//anonymous function 2x, y: = Func (i, J int) (m, n int) {//X Y for function return value return J, i} (1, 9 )//Create anonymous functions directly and execute FMT. fprintf (OS. Stdout, "x =%d y =%d\n", x, y)}
i = 0 1
i = 1 Hello
i = 2 3.14
i = 3 0x4012c0
--------------
i = 0 Hello
i = 1 3.14
i = 2 0x4012c0
--------------
F = 0x401690 f (1,3) = 4
x = 9 y = 1
http://blog.csdn.net/eclipser1987/article/details/17396201
The implementation of the callback function:
Package Mainimport ( "FMT" "StrConv")//declares a savelog type, which actually represents a function definition of type Savelog func (msg string)// The second parameter of this function is a function//This function converts a string to an int type, and if it fails, returns 0 and outputs an error. Func Stringtoint (s string, log savelog) Int64 { if value, err: = StrConv. parseint (s, 0, 0); Err! = Nil { log (err. Error ()) return 0 } else { return value }}//log function implementation func MyLog (msg string) { FMT. PRINTLN ("Find Error:", msg)}//a runtime error output when invoking the second stringtoint func main () { stringtoint ("123", MyLog) Stringtoint ("s", MyLog)}
The result of the operation is:
$ go Build func_callback.go
$./func_callback
Find Error:strconv. Parseint:parsing "s": Invalid syntax