Common Golang Type Conversions

**

1.Type(expression):

**

int(time.Now().Weekday()) //Monday 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

Official introduction to the ParseInt function:

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

- Returns the integer value represented by the string, accepting positive and negative signs.

- base specifies the binary (2 to 36), if base is 0, it will be judged from the string preceding, "0x" is 16, and "0" is 8, otherwise it is 10.

- bitSize specifies that the result must be an integer type with no overflow assignment, 0, 8, 16, 32, 64 representing int, int8, int16, int32, int64, respectively.

- The err returned is of type *NumErr, err.Error = ErrSyntax if the syntax is incorrect, and err.Error = ErrRange if the result exceeds the type range.

The 10 in the FormatInt function, which represents the decimal number.

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 to the 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 grammatical, the function returns a floating point number closest to the value represented by s (rounded using the IEEE754 specification).

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

- The return value err is of *NumErr type, syntactically incorrect, err.Error=ErrSyntax; if the result exceeds the range of representation, the return value f is (+) Inf, err.Error= ErrRange.

Official introduction to the FormatFloat function:

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

- The bitSize represents the source type of f (32:float32, 64:float64), and is rounded accordingly.

- fmt representation formats:'f'(-d D D.D d d d d d),'b' (-dd ddp +d D d, exponent is binary),'e'(-d.d d d d e +d d, decimal index),'E' (-d.d d d d E +d d, decimal index),'g'(e' when exponent is large, otherwise'f'format),'G' (E'when exponent is large, otherwise'f' format).

- prec control precision (excluding exponential 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, it means that f is represented by the minimum number of required numbers.

string and time:

t, _ := time.Parse("2006-01-02 15:04:05", s) //string to time
t, _ := time.ParseInLocation("2006-01-02 15:04:05", s, time.Local) //string to local time
s := t.Format("2006-01-02 15:04:05")         //Time to string

**

3. Type assertion: expression.(Type):

**

expression must be an interface type, and its own type matches the Type type.

expression.(Type) generally returns two values: value and ok, OK for successful matching, true for value, value for value, OK for failed matching, false for value, and no value for value. You can also accept a return value for value directly, but panic for failure:

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

You can also use switch ing and case together to determine the actual type of interface:

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

**

4.JSON:

**

Most data types in Golang can be converted to valid JSON text, except channel s, complex complex numbers, func functions, and so on.

Golang pointers can be implicitly converted. Surface is pointer serialization, internal is pointer value manipulation, or actually serialization of the object pointed to.

Public number: Li Tian Crossing

Keywords: Go JSON

Added by jj20051 on Sat, 07 Mar 2020 02:45:49 +0200