Characters<\/td> | int32 or rune<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n For other types such as Array<\/strong>, Pointer<\/strong>, Structure,<\/strong> etc, type Inference will happen based on the value. Let’s see a working example of the above point. Notice that type of t is correctly inferred as int as the value assigned to it is 123 which is int. Similarly type of u is also correctly inferred as string<\/strong> as the value assigned to it is a string<\/strong>.<\/p>\n\n\n\nAlso notice that the type of variable z<\/strong> is inferred correctly as a struct main.sample<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n var t = 123 \/\/Type Inferred will be int\n var u = \"circle\" \/\/Type Inferred will be string\n var v = 5.6 \/\/Type Inferred will be float64\n var w = true \/\/Type Inferred will be bool\n var x = 'a' \/\/Type Inferred will be rune\n var y = 3 + 5i \/\/Type Inferred will be complex128\n var z = sample{name: \"test\"} \/\/Type Inferred will be main.Sample\n\n fmt.Printf(\"Type: %T Value: %v\\n\", t, t)\n fmt.Printf(\"Type: %T Value: %v\\n\", u, u)\n fmt.Printf(\"Type: %T Value: %v\\n\", v, v)\n fmt.Printf(\"Type: %T Value: %v\\n\", w, w)\n fmt.Printf(\"Type: %T Value: %v\\n\", x, x)\n fmt.Printf(\"Type: %T Value: %v\\n\", y, y)\n fmt.Printf(\"Type: %T Value: %v\\n\", z, z)\n}\n\ntype sample struct {\n name string\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nType: int Value: 123\nType: string Value: circle\nType: float64 Value: 5.6\nType: bool Value: true\nType: int32 Value: 97\nType: complex128 Value: (3+5i)\nType: main.sample Value: &{test}<\/code><\/pre>\n\n\n\n<\/span>Short variable declaration<\/strong><\/span><\/h2>\n\n\n\nGo provides another way of declaring variables which is using the :=<\/strong> operator. When :=<\/strong> operator is used both var <\/strong>keyword and type info can be omitted. Below is the format for such declaration<\/p>\n\n\n\n := <\/code><\/pre>\n\n\n\nType inference will happen as explained above. Let’s see a working example<\/p>\n\n\n\n package main\n\nimport \"fmt\"\n\nfunc main() {\n t := 123 \/\/Type Inferred will be int\n u := \"circle\" \/\/Type Inferred will be string\n v := 5.6 \/\/Type Inferred will be float64\n w := true \/\/Type Inferred will be bool\n x := 'a' \/\/Type Inferred will be rune\n y := 3 + 5i \/\/Type Inferred will be complex128\n z := sample{name: \"test\"} \/\/Type Inferred will be main.Sample\n\n fmt.Printf(\"Type: %T Value: %v\\n\", t, t)\n fmt.Printf(\"Type: %T Value: %v\\n\", u, u)\n fmt.Printf(\"Type: %T Value: %v\\n\", v, v)\n fmt.Printf(\"Type: %T Value: %v\\n\", w, w)\n fmt.Printf(\"Type: %T Value: %v\\n\", x, x)\n fmt.Printf(\"Type: %T Value: %v\\n\", y, y)\n fmt.Printf(\"Type: %T Value: %v\\n\", z, z)\n}\n\ntype sample struct {\n name string\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nType: int Value: 123\nType: string Value: circle\nType: float64 Value: 5.6\nType: bool Value: true\nType: int32 Value: 97\nType: complex128 Value: (3+5i)\nType: main.sample Value: &{test}<\/code><\/pre>\n\n\n\nSome points to be noted about the := operator<\/p>\n\n\n\n - := operator is only available within a function. It is not allowed outside the function.<\/li>
- A variable once declared using := cannot be redeclared using the := operator. So below statement will raise a compiler error “no new variables in the left side of :=”<\/strong> .<\/li><\/ul>\n\n\n\n
a := 8\na := 16<\/code><\/pre>\n\n\n\n- := operator can also be used to declare multiple variables in a single line. See below example<\/li><\/ul>\n\n\n\n
a,b := 1, 2<\/code><\/pre>\n\n\n\n- In case of multiple declaration, := can also be used again for a particular variable if atleast one of the variables on left hand side is new. See below example. Notice that b is again declared using := This is only possible if atleast one of the variable is new which is variable c<\/strong> here. In this case it acts as a assignment for variable b<\/strong><\/li><\/ul>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n a, b := 1, 2\n b, c := 3, 4\n fmt.Println(a, b, c)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n1, 3, 4<\/code><\/pre>\n\n\n\n<\/span>Important Points<\/strong><\/span><\/h1>\n\n\n\n- A unused variable will be reported as a compiler error. GO compiler doesn’t allow any unused variable. This is an optimization in GO. Same is applicable for constant too as we will see later. For eg below program will raise a compiler error<\/li><\/ul>\n\n\n\n
a declared but not used<\/code><\/pre>\n\n\n\npackage main\n\nfunc main() {\n var a = 1\n}<\/code><\/pre>\n\n\n\n- A variable declared within an inner scope having the same name as variable declared in the outer scope will shadow the variable in the outer scope.<\/li><\/ul>\n\n\n\n
package main\n\nimport \"fmt\"\n\nvar a = 123\n\nfunc main() {\n var a = 456\n fmt.Println(a)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n456<\/code><\/pre>\n\n\n\n- Variable Expression – While the declaration variable can also be assigned an expression or a function call. See the below example.
- Variable a<\/strong> is declared with expression 5 +3<\/li>
- Variable b<\/strong> is declared with a function call math.Max(4, 5), whose result will be assigned to b at run time.<\/li><\/ul><\/li><\/ul>\n\n\n\n
package main\nimport (\n \"fmt\"\n \"math\"\n)\nfunc main() {\n a := 5 + 3\n b := math.Max(4, 5)\n fmt.Println(a)\n fmt.Println(b)\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\n8\n5<\/code><\/pre>\n\n\n\n
|