轉換**主要包含時間的轉換、byte型別轉換、string型別轉換、int型別轉換、uint型別轉換、float型別轉換、bool型別轉換以及inte***ce型別轉換。其中conversion.go為實現檔案,conversion_test.go為測試檔案測試通過可放心使用。
呼叫示例可參考測試用例。
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"reflect"
"strconv"
"time"
)const (
time_year = "20060102"
time_hour = "150405"
time_now = "20060102150405"
)func timenow()(to string)
func timeyear()(to string)
func timehour()(to string)
func uint32tobytes(from uint32) (to byte)
func bytestouint32(from byte) (to uint32)
func stringtoint(v string) (d int, err error)
return
int(tmp), err
}func stringtouint(v string) (d uint, err error)
return
uint(tmp), err
}func stringtouint8(v string) (d uint8, err error)
return
uint8(tmp), err
}func stringtouint32(v string) (d uint32, err error)
return
uint32(tmp), err
}func stringtouint64(v string) (d uint64, err error)
func inttostring(from int) (to string)
func int64tostring(from int64) (to string)
func uint32tostring(from uint32) (to string)
func uint64tostring(from uint64) (to string)
func float64tostring(from float64) (to string)
func stringtofloat(v string) (d float32, err error)
func stringtofloat64(v string) (d float64, err error)
func tostring(v inte***ce{}) string
func tostringslice(v inte***ce{}) (string, error) :
r := make(string,0, len(slice))
for _, item := range slice
return r, nil
default:
return
nil, errors.new(fmt.sprintf("cannot convert %v(%v) to string", v, reflect.typeof(v)))
}}func tobool(v inte***ce{}) (bool, error)
case
float32:
return value !=0, nil
case
float64:
return value !=0, nil
case
int8:
return value !=0, nil
case
int16:
return value !=0, nil
case
int32:
return value !=0, nil
case
int:
return value !=0, nil
case
int64:
return value !=0, nil
case
uint8:
return value !=0, nil
case
uint16:
return value !=0, nil
case
uint32:
return value !=0, nil
case
uint:
return value !=0, nil
case
uint64:
return value !=0, nil
default:
return
false, errors.new(fmt.sprintf("cannot convert %v(%v) to bool", v, reflect.typeof(v)))
}}func touint8(v inte***ce{}) (uint8, error)
func touint16(v inte***ce{}) (uint16, error)
func touint(v inte***ce{}) (uint, error)
func touint32(v inte***ce{}) (uint32, error)
func touint64(v inte***ce{}) (uint64, error)
func toint8(v inte***ce{}) (int8, error)
func toint16(v inte***ce{}) (int16, error)
func toint(v inte***ce{}) (int, error)
func toint32(v inte***ce{}) (int32, error)
func toint64(v inte***ce{}) (int64, error)
func tofloat32(v inte***ce{}) (float32, error)
func tofloat64(v inte***ce{}) (float64, error)
}func toint64slice(v inte***ce{}) (int64, error) else
}return r, nil
case string:
r := make(int64,0, len(slice))
for _, item := range slice else
}return r, nil
case inte***ce{}:
r := make(int64,0, len(slice))
for _, item := range slice else
}return r, nil
default:
return
nil, errors.new(fmt.sprintf("cannot convert %v(%v) to int64", v, reflect.typeof(v)))
}}func touint32slice(v inte***ce{}) (uint32, error) else
}return r, nil
case string:
r := make(uint32,0, len(slice))
for _, item := range slice else
}return r, nil
case inte***ce{}:
r := make(uint32,0, len(slice))
for _, item := range slice else
}return r, nil
default:
return
nil, errors.new(fmt.sprintf("cannot convert %v(%v) to uint32", v, reflect.typeof(v)))
}}func touint64slice(v inte***ce{}) (uint64, error) else
}return r, nil
case string:
r := make(uint64,0, len(slice))
for _, item := range slice else
}return r, nil
case inte***ce{}:
r := make(uint64,0, len(slice))
for _, item := range slice else
}return r, nil
default:
return
nil, errors.new(fmt.sprintf("cannot convert %v(%v) to uint64", v, reflect.typeof(v)))
}}
go語言型別轉換
string到int int,err strconv.atoi string string到int64 int64,err strconv.parseint string,10,64 第二個引數為基數 2 36 第三個引數位大小表示期望轉換的結果型別,其值可以為0,8,16,32和64,分別對應 i...
Go 型別強制轉換
強制型別的語法格式 var a t t b 使用括號將型別和要轉換的變數或表示式的值括起來1.x可以直接複製給t型別變數 2.x的型別和t具有相同的底層型別 3.x的型別和t都是未命令的指標型別,並且指標指向的型別具有相同的底層型別 4.x的型別和t都是整型,或都是浮點型,或者都是複數型別 5.x是...
Go 語言型別轉換
型別轉換用於將一種資料型別的變數轉換為另外一種型別的變數。go 語言型別轉換基本格式如下 type name expression type name 為型別,expression 為表示式。例項以下例項中將整型轉化為浮點型,並計算結果,將結果賦值給浮點型變數 package main import...