func main { var coutryCapitalMap map[string]string /* create a map*/ coutryCapitalMap = make(map[string]string) /* insert key-value pairs in the map*/ countryCapitalMap["France"] = "Paris" countryCapitalMap["Italy"] = "Rome" countryCapitalMap["Japan"] = "Tokyo" countryCapitalMap["India"] = "New Delhi" /* print map using keys*/ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } /* test if entry is present in the map or not*/ captial, ok := countryCapitalMap["United States"] /* if ok is true, entry is present otherwise entry is absent*/ if(ok){ fmt.Println("Capital of United States is", capital) }else { fmt.Println("Capital of United States is not present") } }
const Pi float64 = 301418926 const zero = 0.0 const( size int64 = 1024 eof = -1 ) const a, b, c = 3, 4, "ff"// a = 3, b = 4, c = ff, const u, v float32 = 0,3//u=0.0 v=3.0
const( i int u, v, s = 2.1 ,3.0, "bar" ) const a, b, c = 3, 4, "ff"// a = 3, b = 4, c = ff, const u, v float32 = 0,3//u=0.0 v=3.0
指针
声明指针
1 2
var ip *int/* pointer to an integer */ var fp *float32 /* pointer to a float */
举例
1 2 3 4 5 6 7 8 9 10
package main import"fmt" func main(){ var a int= 20 var ip *int/* 声明一个指针 */ ip = &a /* 给指针赋值 */ fmt.Printf("Address of a variable: %x\n", &a ) /* 输出为:10328000 */ fmt.Printf("Address stored in ip variable: %x\n", ip ) /* 输出为:10328000 */ fmt.Printf("Value of *ip variable: %d\n", *ip ) /* 输出为:20 */ }
结构体
1 2 3 4 5 6
type Books struct { title string author string subject string book_id int }