# Constants

Constants in Go are declared similar to variables but with `const` keyword. Constant in Go can appear anywhere variable can appear and does not have shorthand as var has. The simplest way to define a string constant would be

```go
const str = "Hey, I am defined as constant"
```

Yes, I know what you may be thinking, should the constant name not be all capital? No, in Go, it's not mandatory, constant can be all capital, came cased or usual snake cased. There are no such specific guidelines on how constants should be named

But for the sake of consistency, I would suggest that one should use camel case while naming constants. But how do constants differ from variables in Go? In Go, like any other language constants can not be reassigned. To test this hypothesis, let's run the following code in Go Playground.

```go
package main
import "fmt"
const Pi = 3.14
func main() {
    const World = "世界"
    fmt.Println("Hello", World)
    fmt.Println("Happy", Pi, "Day")
    World = "World"
}
```

The above code, when executed would error. Because constants can not be reassigned

```go
./prog.go:8:8: cannot assign to World

Go build failed.
```

All set, right? :) No

In Go, a numeric constant has no type, until its given one. One need to force typecast const to have it its type, like following

```go
const Big = 1 << 60
fmt.Println(int64(Big))
// => 1152921504606846976
```

{% hint style="info" %}
In Go, a numeric constant has no type, until its given one.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://golang.bagwanpankaj.com/constants.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
