Characters<\/td> | int32 or rune<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n When you print any untyped constant using fmt.Printf <\/strong>it will print the default hidden type. See below program and output for both unnamed and named untyped constant.<\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n \/\/Unanamed untyped constant\n fmt.Printf(\"Type: %T Value: %v\\n\", 123, 123)\n fmt.Printf(\"Type: %T Value: %v\\n\", \"circle\", \"circle\")\n fmt.Printf(\"Type: %T Value: %v\\n\", 5.6, 5.6)\n fmt.Printf(\"Type: %T Value: %v\\n\", true, true)\n fmt.Printf(\"Type: %T Value: %v\\n\", 'a', 'a')\n fmt.Printf(\"Type: %T Value: %v\\n\", 3+5i, 3+5i)\n\n \/\/Named untyped constant\n const a = 123 \/\/Default hidden type is int\n const b = \"circle\" \/\/Default hidden type is string\n const c = 5.6 \/\/Default hidden type is float64\n const d = true \/\/Default hidden type is bool\n const e = 'a' \/\/Default hidden type is rune\n const f = 3 + 5i \/\/Default hidden type is complex128\n\n fmt.Println(\"\")\n fmt.Printf(\"Type: %T Value: %v\\n\", a, a)\n fmt.Printf(\"Type: %T Value: %v\\n\", b, b)\n fmt.Printf(\"Type: %T Value: %v\\n\", c, c)\n fmt.Printf(\"Type: %T Value: %v\\n\", d, d)\n fmt.Printf(\"Type: %T Value: %v\\n\", e, e)\n fmt.Printf(\"Type: %T Value: %v\\n\", f, f)\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)\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)<\/code><\/pre>\n\n\n\nThe above program prints int32<\/strong> instead of rune as rune is an alias for int32<\/strong><\/p>\n\n\n\nThe default type of a named or unnamed constant type will become type of a variable they are assigned to . For example in below code variable a<\/strong> will get its type from the default type of unnamed constant 123<\/strong> which is int<\/strong>.<\/p>\n\n\n\nvar a = 123<\/code><\/pre>\n\n\n\nLet’s see a program illustrating above points for all unnamed type constant<\/p>\n\n\n\n package main\nimport \"fmt\"\nfunc main() {\n \/\/Untyped\n var u = 123 \/\/Default hidden type is int\n var v = \"circle\" \/\/Default hidden type is string\n var w = 5.6 \/\/Default hidden type is float64\n var x = true \/\/Default hidden type is bool\n var y = 'a' \/\/Default hidden type is rune\n var z = 3 + 5i \/\/Default hidden type is complex128\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}<\/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)<\/code><\/pre>\n\n\n\nNow the question which comes to the mind is what is the use of untyped constant. The use of untyped constant is that the type of the constant will be decided depending upon the type of variable they are being assigned to. Sounds confusing? Let’s see with an example.<\/p>\n\n\n\n Pi <\/strong>constant value in math package is declared as below.<\/p>\n\n\n\nconst Pi = 3.14159265358979323846264338327950288419716939937510582097494459<\/code><\/pre>\n\n\n\nNotice that the type is not specified it only has a hidden default type (which is float64<\/strong> here). Let’s see a code<\/p>\n\n\n\npackage main\nimport (\n \"fmt\"\n \"math\"\n)\nfunc main() {\n var f1 float32\n var f2 float64\n f1 = math.Pi\n f2 = math.Pi\n\n fmt.Printf(\"Type: %T Value: %v\\n\", math.Pi, math.Pi)\n fmt.Printf(\"Type: %T Value: %v\\n\", f1, f1)\n fmt.Printf(\"Type: %T Value: %v\\n\", f2, f2)\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nType: float64 Value: 3.141592653589793\nType: float32 Value: 3.1415927\nType: float64 Value: 3.141592653589793<\/code><\/pre>\n\n\n\nNotice the above program.<\/p>\n\n\n\n - Due to the untyped nature of math.Pi<\/strong> constant it can be assigned to a variable of type float32<\/strong> as well as float64<\/strong>. This is otherwise not possible in GO after type is fixed.<\/li><\/ul>\n\n\n\n
- When we print the type of math.Pi <\/strong>, it prints the default type which is float64<\/strong><\/li><\/ul>\n\n\n\n
Depending upon use case an untyped constant can be assigned to a low precision type (float32) or a high precision type(float64)<\/p>\n\n\n\n <\/span>Naming Conventions<\/strong><\/span><\/h1>\n\n\n\nNaming conventions for constant is the same as naming conventions for variables.<\/p>\n\n\n\n - A constant name can only start with a letter or an underscore. It can be followed by any number of letters, numbers or underscores after that<\/li><\/ul>\n\n\n\n
<\/span>Global Constant<\/strong><\/span><\/h1>\n\n\n\nLike any other variable, a constant will be global within a package if it is declared at the top of a file outside the scope of any function. For example, in the below program name will be a global constant available throughout the main<\/strong> package in any function. Do note that the const name will not be available outside the main package. For it to be available outside the main package it has to start with a capital letter. <\/p>\n\n\n\nSee the below code. It also shows the example of a local constant within a package.<\/p>\n\n\n\n package main\n\nimport \"fmt\"\n\nconst name = \"test\"\n\nfunc main() {\n const a = 8\n fmt.Println(a)\n testGlobal()\n}\n\nfunc testGlobal() {\n fmt.Println(name)\n \/\/The below line will give compiler error as a is a local constant\n \/\/fmt.Println(a)<\/code><\/pre>\n\n\n\n<\/span>Types of Constants<\/strong><\/span><\/h1>\n\n\n\nConstant can be of four types:<\/p>\n\n\n\n - Numeric<\/li>
- String<\/li>
- Character<\/li>
- Boolean<\/li><\/ul>\n\n\n\n
<\/span>String Constant<\/strong><\/span><\/h2>\n\n\n\nIn go string constant is represented in two ways<\/p>\n\n\n\n - Any value that is enclosed between double quotes<\/li><\/ul>\n\n\n\n
|