Common types conversion of golang





int( time.Now (). Weekday()) // week to int
int( time.Now (). Month ()) // month to int
var a float64
a = 3.1
B: = int (a) // float64 to int
var a int
a = 1
B: = Int64 (a) // int to Int64


2. Strconv package:

String and int, int32, Int64:

i, _  :=  strconv.Atoi (s) // string to int
s :=  strconv.Itoa (i) // int to string
i, _  :=  strconv.ParseInt (s, 10, 32) // string to int32
ii := int32(i)
i, _  :=  strconv.ParseInt (s, 10, 64) // string to int32
s :=  strconv.FormatInt (I, 10) // Int64 to string

ParseInt function official introduction:

func ParseInt(s string, base int, bitSize int) (i int64, err error)

– returns an integer value represented by a string, accepting a sign.

– base specifies the base (2 to 36). If base is 0, it will be judged from the front of the string that “0x” is hexadecimal, “0” is octal, otherwise it is 10.

– bitsize specifies that the result must be an integer type with no overflow assignment. 0, 8, 16, 32 and 64 represent int, int8, int16, int32 and Int64 respectively.

The err returned is of type * numerr. If the syntax is incorrect, err.Error =Errsyntax; if the result is out of type range err.Error = ErrRange。

The 10 in the formatin function represents the decimal.

String and float32, float64:

f, _  :=  strconv.ParseFloat (s, 32) // string to float32
ff := float32(f)
f, _  :=  strconv.ParseFloat (s, 64) // string to float64
s :=  strconv.FormatFloat (F, 'f', - 1, 64) // float64 to string

Official introduction of parsefloat function:

func ParseFloat(s string, bitSize int) (f float64, err error)

Parses a string representing a floating-point number and returns its value.

– if s is syntactically correct, the function returns the floating-point number closest to the value represented by s (rounded using the IEEE754 specification).

– bitsize specifies the expected receive type, 32 is float32 (the return value can be assigned to float32 without changing the exact value), 64 is float64.

The return value err is of type * numerr, with incorrect syntax, err.Error=ErrSyntax If the result is out of the range, the return value f is ± inf, err.Error= ErrRange。

Official introduction of formatflot function:

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

– bitsize indicates the source type of F (32: float32, 64: float64), which will be rounded accordingly.

– FMT format: ‘f’ (- ddd.dddd )In this paper, we use the following expressions: A, B ‘(- ddddp ± DDD, index is binary),’e’ (- d.dddde ± DD, decimal index),’e ‘(- d.dddde ± DD, decimal index),’g’ (use ‘e’ format when index is large, otherwise ‘f’ format),’g ‘(use’ e ‘format when index is large, otherwise’ f ‘format).

– prec control precision (excluding index part): for ‘f’,’e ‘,’e’, it represents the number of digits after the decimal point; for ‘g’,’g ‘, it controls the total number of digits. If prec is – 1, then f is represented by the minimum number of necessary numbers.

String and time:

t, _  :=  time.Parse ("2006-01-02 15:04:05", s) // String transfer time
t, _  :=  time.ParseInLocation ("2006-01-02 15:04:05", s,  time.Local )// String local time
S: = T. format ("2006-01-02 15:04:05") // time to string


3. Type assertion: expression


Expression must be an interface type and its own type matches the type.

The return values of expression. (type) are generally two: value and OK. If the match succeeds, OK is true, value has a value, if the match fails, OK is false, and value has no value. You can also directly accept a return value of value, but if the match fails, you can directly panic:

func main() {
  var a interface{} = 100
  if aa, ok := a.(int); ok {

It can also be used in combination with switch and case to determine the actual type of interface

func main() {
  var t interface{} = 100
  switch i := t.(type) {
  case float32:
    fmt.Printf ("type of I% T, value of I% v, n", I, I)
  case float64:
    fmt.Printf ("type of I% T, value of I% v, n", I, I)
  case int:
    fmt.Printf ("type of I% T, value of I% v, n", I, I)
  case bool:
    fmt.Printf ("type of I% T, value of I% v, n", I, I)
  case string:
    fmt.Printf ("type of I% T, value of I% v, n", I, I)
    fmt.Println (other types)




Most of the data types in golang can be converted into valid JSON text, except for channel channel, complex complex number, func function, etc.

The golang pointer can be converted implicitly. On the surface, the pointer is serialized. On the inside, the pointer is used for value operation. In fact, the object is serialized.

Official account: Li Tian intersection

Recommended Today

Rust Study RoadMap

background In recent months, I began to learn rust, and began to write some code with rust. Now, I don’t have a deep understanding of rust, but it’s OK to write some code everyday, and I can learn the next step as needed. In the process of learning, the author also read a lot of […]