Learning Golang Language (3): Type--Boolean and numeric types

Source: Internet
Author: User
Tags learn golang
This is a creation in Article, where the information may have evolved or changed.

Follow the code to learn Golang language. Today explains the basic types of Golang languages, and describes Boolean types and numeric types.

Learning Golang Language (1): Hello World

Learning Golang Language (2): Variables

Learning Golang Language (3): Type--Boolean and numeric types

Learning Golang Language (4): Type--string

Learning Golang Language (5): Type--array


Boolean type

The Boolean type is bool. The Go language provides built-in Boolean values of True and flase. The go language supports standard logic and comparison operations. The result of these operations is a Boolean value.

Boolean values and expressions can be used in the IF statement, in the condition of the For statement, and in the case of the switch statement.

Logical operators:

!: logical non-operator;

|| : Logical OR operator;

&&: Logic and operators

The comparison operator.

<,>, ==,!=, <=, >=


Numeric type

----shaping and floating-point

The Go language provides a number of built-in numeric types. Well-known types such as int, this type determines the appropriate length based on your system. On a 32-bit system is 32 bits and 64 bits on 64-bit systems. So in the go language, int and int32 are different types. If you want to be clear about its length, you can use Int32 or int64 and so on.

Complete integer types (symbols and unsigned) are Int8,int16,int32,int64 and Byte,uint8,uint16,uint32,uint64. Where Byte is the alias of the Uint8.

The values for floating-point types are float32 and float64 (no float type). 64-bit integers and floating-point numbers are always 64-bit, even on 32-bit architectures.

The default assignment for shaping variables is 0, and the default assignment for floating-point variables is 0.0

It is important to note that these types are all independent, and mixing these types to assign values to variables can cause compiler errors. For example:

Package MainFunc Main () {  var a int//  general integer type  var b int32//32-bit integer type  a =  b =a + a   //mix These types are illegal, this Causes compilation exception  B = B + 5  //5 is constant (undefined type), so no problem}

Type conversions are required if numeric operations or comparison operations are performed between different numeric types. The type is typically converted to the largest type to prevent loss of precision. Type conversions take the form of type (value). When converting a type to a small type, we can create the appropriate function to prevent the loss of precision. Example: Convert int to Uint8

Func uint8fromint (n int) (uint8, error) {  if 0 <= n && n <= Math. MaxUint8 {//conversion is safe      return uint8 (n), nil  }  return 0, FMT. Errorf ("%d is out of the Uint8 range", N)}

----plural types

Go native support plural. There are two types of complex numbers: complex64 (the real part is a float32) and complex128 (the real part of the imaginary part is a float64). The form of the plural is: Re+im I. where re is the real part, IM is imaginary part.

The complex number can be created using the built-in complex () function or the represented containing the imaginary part value. The various parts of the complex number can be obtained using the built-in functions real () and the Imag () function.

For example:

  var C1 complex64 = 5 + 10i  FMT. Printf ("The value is:%v", C1)   //This will output: 5 + 10i   c: = Complex (50,100);   Fmt. Printf ("The value is:%v", C1)   //This will output: 100i

The complex number supports all arithmetic operators. The only comparison operation symbol that can be used for complex numbers is = = and! =.

The standard library has a complex package MATH/CMPLX that provides a variety of common operation functions for complex numbers.

If you do not need to consider memory issues, use the complex128 type as much as possible, because all functions in the standard library use the complex128 type.

---------------------------------------------

Welcome to pay attention to code surgery! Learn Golang together.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.