About the basic type of Go

Basic type

Logical value type

The logical value is represented by bool type. As shown below, the value is ** true ** if true and ** false ** if false. You can also define it with type inference.

var a bool  //Define variable a as bool type
a = true    //Assign true to variable a
a := true

Numeric type

Integer types are available as numeric types, and there are two types: ** signed integer type ** and ** unsigned integer type **.

Signed integer types include int8, int16, int32, and int64, and the numbers represent the size. There are uint8 (byte) `` `, uint16, uint32, uint64types of unsigned integer types, and the number is also the size. Represents. Unlike signed,uint8 can also be defined with an alias of `` `byte type, which behaves the same as uint8.

Also, in the 64-bit implementation of Go, the int type behaves the same as the int64, but as shown below, the variables that represent the same 64-bit integer are However, because the types are different, implicit type conversion will result in compilation errors.

a := int(10)
b := int64(5)
b = a  //Compile error
fmt.Printf("%v\n", b)

In this way, if you try to perform type conversion implicitly, you will get a compile error. However, if you specify the type firmly as shown below and perform type conversion explicitly, you can execute it without causing a compile error.

a := int(10)
b := int64(a)
fmt.Printf("%v\n", b)

This method allows you to process numbers that exceed the maximum type size.

a := int8(128)  //Compile error
fmt.Printf("%v\n", a)
a := 128
b := int8(a)
fmt.Printf("%v\n", b)

It is now possible to process at 128, which exceeds the maximum value of ** int8, 127 **. By doing this explicitly, ** overflow ** occurs on the binary representation when 1 is added to the maximum value 127, ** wraparound **, and the minimum value. Because it will return to. So, in this case, the output value will be -128 **, which is the minimum value of ** int8.

By importing and using the ** math package ** as an up-around measure, you can write code while being aware of the overflow of digits.

Floating point type

There are two types of floating point types: `float32: single precision floating point ``` and `float64: double precision floating point ```. By the way, float32 is less accurate than float64, and if you don't consider memory usage efficiency, there seems to be little merit in using it unless you have a specific reason.

a := 1.0           //float64
b := float32(1.0)  //float32

Floating-point types also have constants that represent a range of numbers in the math package, and you need to perform explicit type conversion if you want to use floating-point literals to take different types of values. However, the decimal point is truncated.

Next is the special value that makes the operation impossible. The output results of the following operations have the meanings of + Int (** positive infinity ), -Int ( negative infinity ), and NaN ( non-number **), respectively.

1.0 / 0.0   //+Int
-1.0 / 0.0  //-Int
0.0 / 0.0   //NaN

Complex type

A is the real part, b is the imaginary part, and i is the imaginary unit, which is represented by the literal ** a + bi **, and has two types, complex64 and `complex128```. Is available. Float32``` is used for the real part and imaginary part of complex64 type, respectively, and `` float64``` is used for the real part and imaginary part of complex128 type.

The following is an assignment method using complex literals. By the way, the + sign is just a part of the literal, not an operator. You can also use the complex function to return and generate each value by taking the arguments of the real part and the imaginary part.

a := 1.0 + 2i
a := complex(1.0, 2)

There is also a function to retrieve the real and imaginary values of this literal.

a := 1.0 + 2i

real(a)  //1.0
imag(a)  //2

rune type

This is a type that represents a character, and the character enclosed in single quotation marks is represented by Unicode code points and output. You can also represent Unicode characters by adding a prefix that starts with a backslash. There are also escape sequences for string representations. It is defined as another name int32 and works the same.

a := 'Example'
fmt.Printf("%v", a)  //20363
a := '\U00101234'
fmt.Printf("%v", a)  //1053236

String type

It is defined as a string type and provides a string literal enclosed in double quotes.

a := "example"
fmt.Printf("%v", a)  //example
a := "\u65e 5 pieces\U00008a9e"
fmt.Printf("%v", a)  //Japanese

Another form of string literal is ** RAW string literal **. It uses back quotes and has the property of supporting multiple lines of strings and not processing those characters.

a := '
example
example
example
'
fmt.Printf("%v", a)  //example
example
example

Finally

This time we have summarized the basic types of Go! I think you could understand not only the surface of the basic type itself, but also the meaning and mechanism to some extent.

I will continue to output at this rate!

Recommended Posts

About the basic type of Go
About the ease of Python
About the components of Luigi
About the features of Python
Ruby expert learned the basic grammar of Go language
About the return value of pthread_mutex_init ()
About the return value of the histogram.
About the upper limit of threads-max
About the behavior of yield_per of SqlAlchemy
About the size of matplotlib points
About the basics list of Python basics
About the behavior of enable_backprop of Chainer v2
About the virtual environment of python version 3.7
About the arguments of the setup function of PyCaret
[python] [meta] Is the type of python a type?
About the Normal Equation of Linear Regression
Python Basic Course (at the end of 15)
Reading comprehension of "The Go Memory Model"
About reference type
About the test
Comparing the basic grammar of Python and Go in an easy-to-understand manner
About Go functions
About Go Interface
About the queue
About the accuracy of Archimedean circle calculation method
About the behavior of copy, deepcopy and numpy.copy
About the X-axis notation of Matplotlib bar graphs
Check the type of the variable you are using
About the processing speed of SVM (SVC) of scikit-learn
A note about the python version of python virtualenv
This is the only basic review of Python ~ 1 ~
This is the only basic review of Python ~ 2 ~
About the development contents of machine learning (Example)
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
This is the only basic review of Python ~ 3 ~
About the behavior of Queue during parallel processing
About the * (asterisk) argument of python (and itertools.starmap)
[Python] Determine the type of iris with SVM
[Go] Basic grammar ① Definition
The beginning of cif2cell
Learn about Go slices
A memorandum about the warning of the pylint output result
[Go] Basic grammar ② Statement
About all of numpy
I tried to summarize the basic form of GPLVM
The meaning of self
Basic operation of pandas
Note about pointers (Go)
Basic usage of flask-classy
About assignment of numpy.ndarray
Basic usage of Jinja2
Go Quiz: Type assertions
Make a note of the list of basic Pandas usage
the zen of Python
Think about the next generation of Rack and WSGI
About testing in the implementation of machine learning models
About the inefficiency of data transfer in luigi on-memory
Basic operation of Pandas
Basic usage of SQLAlchemy
The story of sys.path.append ()