> For the complete documentation index, see [llms.txt](https://minilabmemo.gitbook.io/golang-memo/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minilabmemo.gitbook.io/golang-memo/basic/go-basic/xing-tai-types.md).

# base Types

### 宣告

```go
//變量宣告
var 變量名 變量類型
var a, b *int//指针类型

var foo string
foo := "Hello"
var foo string = "Hello"

var (
  foo string = "Hello"
    bar int = 100
)

//常量宣告
常量是一個簡單值的標識符，在程序運行時，不會被修改的量。
const identifier [type] = value
```

#### new

* <mark style="background-color:blue;">new 會並且返回儲存位址</mark> **且**自動用 [zeroed value](#zero-values) 來初始化型別
* 但要注意像是<mark style="background-color:orange;">map/slice/chan等會是nil，直接使用可能會引發錯誤，通常會另外make來宣告使用</mark>。
* <mark style="background-color:yellow;">map/slice/chan 常用make宣告時就不會拿到指標，要拿到指標請用new</mark>

```
func main() {
	myInt := new(int)
	fmt.Println(myInt). //記憶體位置0xc00009c000
	fmt.Println(*myInt).  //初始值 0 
	fmt.Printf("%#v", myInt) //%#v 先输出结构体名字值，再输出结构体（字段名字+字段的值） (*int)(0xc00009c000)
}
```

* 使用new(struct)雖然可以快速初始化，但是無法一開始就給指定內容，因此<mark style="background-color:yellow;">常使用\&Struct{Field:xxx}來使用</mark>．

### 型態用法

* <mark style="color:blue;">值類型</mark>包括 int、float、bool、string、struct 以及數組(array)
* <mark style="color:blue;">引用類型</mark>包括指針(Pointer)、切片(slice)、map、通道(chan)
* 可以透過 fmt.Printf("Type: %T ", xx) 印出該類型的type
* 可以通過 math.MaxInt64、math.MinInt64 的方式得到預定義的某類型最大最小值。

### Zero values

* <mark style="color:blue;">**0**</mark> for **numeric** types
* <mark style="color:blue;">**false**</mark> for the **boolean** type
* <mark style="color:blue;">**""**</mark> (the empty **string**) for strings.
* <mark style="color:blue;">**nil**</mark> for **Pointer/Interface/Slice/Map/Channel/Function**

### Basic types

* 基本類型如下：

```go
bool
string
int、int8、int16、int32、int64
uint、uint8、uint16、uint32、uint64、uintptr
byte // uint8 的别名
rune // int32 的别名 代表一个 Unicode 码
float32、float64
complex64、complex128
```

* 範圍與大小

| type                                                                                                                                                                    | Size                                                                                                                                          | range                                                                                                                                                                   |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| bool                                                                                                                                                                    | <mark style="background-color:green;">8bit(1 byte)</mark>                                                                                     | 0,1                                                                                                                                                                     |
| string                                                                                                                                                                  | <mark style="background-color:red;">16 byte \[by StringHeader]</mark>                                                                         | <p>a string is a sequence of bytes.<br>golang 字符串是utf8编码</p>                                                                                                            |
| <p>//Signed Integers<br>//Unsigned Integers</p>                                                                                                                         |                                                                                                                                               |                                                                                                                                                                         |
| <p>int<br>uint<br>uintptr</p>                                                                                                                                           | <p>32 bits wide <mark style="color:blue;">on 32-bit systems</mark></p><p>64 bits wide <mark style="color:blue;">on 64-bit systems.</mark></p> |                                                                                                                                                                         |
| <p><mark style="color:red;">int8</mark><br><mark style="color:blue;">uint8</mark><br><mark style="color:blue;">byte</mark> // alias for uint8</p>                       | <p><mark style="background-color:green;">8bit (1 byte)</mark><br><mark style="color:orange;">2的8次方=256</mark></p>                             | <p><mark style="color:red;">-128 to 127</mark><br><mark style="color:blue;">0 and 255</mark> , <mark style="color:blue;">binary:00000000\~11111111</mark> (8bit)</p>    |
| <p><mark style="color:red;">int16</mark><br><mark style="color:blue;">uint16</mark></p>                                                                                 | <p><mark style="background-color:purple;">16bit (2 byte)</mark><br><mark style="color:orange;">2的16次方=65536</mark></p>                        | <p><mark style="color:red;">-32,768 and 32,767</mark><br><mark style="color:red;">(-2的15次方 to 2的15次方-1 )</mark><br><mark style="color:blue;">0 and 65535</mark></p>     |
| <p><mark style="color:red;">int32</mark><br><mark style="color:red;">rune</mark> // alias for int32 (Unicode code point)<br><mark style="color:blue;">uint16</mark></p> | <mark style="background-color:yellow;">32bit (4 byte)</mark>                                                                                  | <p><mark style="color:red;">-2,147,483,648 and 2,147,483,647.</mark><br>#<mark style="color:blue;">0 and 4,294,967,295</mark></p>                                       |
| <p><mark style="color:red;">int64</mark><br><mark style="color:blue;">uint64</mark></p>                                                                                 | <mark style="background-color:orange;">64bit (8 byte)</mark>                                                                                  | <p>#<mark style="color:red;">-9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.</mark><br>#<mark style="color:blue;">0 and 18,446,744,073,709,551,615</mark></p> |
| float32                                                                                                                                                                 | <mark style="background-color:yellow;">32bit (4 byte)</mark>                                                                                  | //1.401298464324817070923729583289916131280e-45 and 3.40282346638528859811704183484516925440e+38.                                                                       |
| float64                                                                                                                                                                 | <mark style="background-color:orange;">64bit (8 byte)</mark>                                                                                  | //4.940656458412465441765687928682213723651e-324 and 1.797693134862315708145274237317043567981e+308.                                                                    |
| complex64                                                                                                                                                               | <mark style="background-color:orange;">64bit (8 byte)</mark>                                                                                  |                                                                                                                                                                         |
| complex128                                                                                                                                                              | <mark style="background-color:red;">128bit (16 byte)</mark>                                                                                   |                                                                                                                                                                         |

###

#### 參考

| ref.                                                                                                                                                                  | note |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---- |
| [go範例程式與範圍](https://www.callicoder.com/golang-basic-types-operators-type-conversion/)                                                                                 |      |
| <p><a href="https://medium.com/d-d-mag/golang-%E7%AD%86%E8%A8%98-make-%E8%88%87-new-%E7%9A%84%E5%B7%AE%E5%88%A5-68b05c7ce016">golang 筆記：make 與 new 的差別</a></p><p></p> |      |
|                                                                                                                                                                       |      |

*
