Untyped named constant<\/li><\/ul>\n\n\n\nIt is the same case with numeric as well.<\/p>\n\n\n\n
A untyped integer constant (both named and unnamed) can be assigned to int<\/strong> types, float<\/strong> types and complex<\/strong> . This is because an int value can be int or float or complex. For eg int value 123<\/strong> can be<\/p>\n\n\n\n- A int<\/strong> whose value is 123<\/li>
- A float<\/strong> whose value is 123.0<\/li>
- A complex<\/strong> whose imaginary part is 0<\/li><\/ul>\n\n\n\n
On the basis of similar logic a untyped float<\/strong> constant can be assigned to all floats<\/strong> and complex<\/strong> types but not integer<\/strong> because for eg a float 5.3 cannot be an integer.<\/p>\n\n\n\n
On the basis of similar logic a untyped comple<\/strong> constant can be assigned to complex<\/strong> types but not integer <\/strong>and float<\/strong> because for eg a float 5i+3 cannot be an integer<\/strong> or a float<\/strong> <\/p>\n\n\n\nLet’s see a program to understand it<\/p>\n\n\n\n
<\/span>Example<\/strong><\/span><\/h1>\n\n\n\nSee below program illustrating the above point. In the program we have example for<\/p>\n\n\n\n
- Typed integer constant<\/li>
- Untyped unnamed integer constant<\/li>
- Untyped named integer constant<\/li><\/ul>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n\t\/\/Typed int constant\n\tconst aa int = 123\n\tvar uu = aa\n\tfmt.Println(\"Typed named integer constant\")\n\tfmt.Printf(\"uu: Type: %T Value: %v\\n\\n\", uu, uu)\n\n\t\/\/Below line will raise a compilation error\n\t\/\/var v int32 = aa\n\n\t\/\/Untyped named int constant\n\tconst bb = 123\n\tvar ww = bb\n\tvar xx int32 = bb\n\tvar yy float64 = bb\n\tvar zz complex128 = bb\n\tfmt.Println(\"Untyped named integer constant\")\n\tfmt.Printf(\"ww: Type: %T Value: %v\\n\", ww, ww)\n\tfmt.Printf(\"xx: Type: %T Value: %v\\n\", xx, xx)\n\tfmt.Printf(\"yy: Type: %T Value: %v\\n\", yy, yy)\n\tfmt.Printf(\"zz: Type: %T Value: %v\\n\\n\", zz, zz)\n\n\t\/\/Untyped unnamed int constant\n\tvar ll = 123\n\tvar mm int32 = 123\n\tvar nn float64 = 123\n\tvar oo complex128 = 123\n\tfmt.Println(\"Untyped unnamed integer constant\")\n\tfmt.Printf(\"ll: Type: %T Value: %v\\n\", ll, ll)\n\tfmt.Printf(\"mm: Type: %T Value: %v\\n\", mm, mm)\n\tfmt.Printf(\"nn: Type: %T Value: %v\\n\", nn, nn)\n\tfmt.Printf(\"oo: Type: %T Value: %v\\n\", oo, oo)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nTyped named integer constant\nuu: Type: int Value: 123\n\nUntyped named integer constant\nww: Type: int Value: 123\nxx: Type: int32 Value: 123\nyy: Type: float64 Value: 123\nzz: Type: complex128 Value: (123+0i)\n\nUntyped unnamed integer constant\nll: Type: int Value: 123\nmm: Type: int32 Value: 123\nnn: Type: float64 Value: 123\noo: Type: complex128 Value: (123+0i)<\/code><\/pre>\n\n\n\nNow above program shows example of a<\/p>\n\n\n\n