1. 程式人生 > >go語言基礎之複數型別

go語言基礎之複數型別

1、複數型別

示例1:

package main //必須有一個main包

import "fmt"

func main() {
	var t complex128 //宣告
	t = 2.1 + 3.14i  //賦值
	fmt.Println("t = ", t)

	//自動推導型別
	t2 := 3.3 + 4.4i
	fmt.Printf("t2 type is %T\n", t2)

	//通過內建函式,取實部和虛部
	fmt.Println("real(t2) = ", real(t2), ", imag(t2) = ", imag(t2))

}

#執行結果:

t =  (2.1+3.14i)
t2 type is complex128
real(t2) =  3.3 , imag(t2) =  4.4