Golang study notes [5] integral type

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


Golang has a large number of integral types, but generally only use int or Uintt.


Package Mainimport ("FMT" "unsafe") Func main () {var i1 int8 = 1  //1 bytevar i2 int16 = 2//2 Bytevar i3 int32 = 3//4 Bytevar i4 Int64 = 4//8 Bytevar i5 int = 5   //32 bit: 4 64 bit: 8var I6 uint8 = 1  //1 Bytevar i7 uint16 = 2//2 Byteva R i8 uint32 = 3//4 Bytevar I9 UInt64 = 4//8 Bytevar i10 UINT = 5  //32-bit: 4 64-bit: 8var i11 byte = 1//equivalent to Uint8 1 Bytev Ar i12 rune = 3//equivalent to Int32 4 bytevar i13 uintptr = 6//32 bit: 4 64 bit: 8FMT. Println (unsafe. Sizeof (i13))//8fmt. Println (I1, I2, i3, I4, i5, I6, i7, i8, I9, i10, I11, I12, i13)//1 2 3 4 5 1 2 3 4 5 1 3 6}


The int type and int32 or int64 cannot be added directly, only the same type (the same type name) can be added and reduced, and Golang does not support implicit conversions.

Package MainFunc Main () {var i1 int = 1var i2 int32 = 2var i3 int64 = 3i4: = I1 + I2}

. \main.go:9:Invalidoperation:i1+  i2 (mismatchedtypesint   andint32)


Use INT (variable name cast)

Package Main
Import(
  "FMT"
)
FuncMain (){
  var i1 int=1
  var i2 int32=2
  var i3 int64=3
 i4: =i1+int(i2)//int32 cast to int 
 i5: =i1+int(i3)//int64 cast to int
 FMT. Println (I4,i5)
}









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.