Variables and Constants
In Go, variables and constants are strongly typed
. To declare a constant, you use the const
keyword, and for variables, you use var
. Go can also automatically infer variable types
, providing various syntaxes for declaring variables and constants, as shown below.
Simple Vars Example
$ go run src/fundamentals/vars.go
Starting basic variables
Print A const: I'm a constant
Print B var: false
NOTICE: B was infer as false
Different ways to declare a variable:
Full declaration: A
Short hand: B
package main
import "fmt"
const A = "I'm a constant"
var B bool
func main() {
fmt.Println("Starting basic variables")
fmt.Println("Print A const: ", A)
fmt.Println("Print B var: ", B)
fmt.Println("NOTICE: B was infer as false")
println()
fmt.Println("Different ways to declare a variable: ")
var full_declare string = "A"
fmt.Println("Full declaration: ", full_declare)
short_hand := "B"
fmt.Println("Short hand: " + short_hand)
}
Checking Default Infered Types
Go assigns a default zero value to any variable that is declared but not assigned a value. Having an explicit zero value makes code clearer and removes a source of bugs.
Default Infered Types
Explict Type Conversion
In Go, automatic type promotion between variables is not permitted. Whenever variable types do not align, explicit type conversion is necessary. Even when dealing with integers and floats of varying sizes, they must be converted to the same type before interaction. This approach ensures clarity regarding the intended data type.
Since all type conversions in Go are explicit, you cannot treat another Go type as a boolean
. Unlike some languages where a nonzero number or a nonempty string can be interpreted as true, Go doesn't support such implicit conversions
. No other type can be directly converted to a boolean, either implicitly or explicitly
. If there's a need to convert from another data type to boolean, it's necessary to use one of the comparison operators explicitly
.
Converting Types
$ go run src/fundamentals/type_conversion.go
Example: Converting Int and Float
Sum1: 25.5
Sum2: 25
Example: Converting Int and Byte
Sum3: 110
Sum4: 110
Right way to compare string
Right way to compare int
package main
import "fmt"
func intFloatConversion() {
fmt.Println("Example: Converting Int and Float")
x := 10
y := 15.5
var sum1 float64 = y + float64(x)
var sum2 int = x + int(y)
fmt.Println("Sum1: ", sum1)
fmt.Println("Sum2: ", sum2)
fmt.Println()
}
func intByteConversion() {
fmt.Println("Example: Converting Int and Byte")
x := 10
var y byte = 100
var sum3 int = x + int(y)
var sum4 byte = y + byte(x)
fmt.Println("Sum3: ", sum3)
fmt.Println("Sum4: ", sum4)
fmt.Println()
}
func stringIntBoolComparison() {
var str string = ""
// if str -> will not work
// if str == true -> will not work
if str == "" {
fmt.Println("Right way to compare string")
}
// if intNum -> will not work
// if intNum == true -> will not work
var intNum int = 1
if intNum == 1 {
fmt.Println("Right way to compare int")
}
}
func main() {
intFloatConversion()
intByteConversion()
stringIntBoolComparison()
}
Beyond Constants
In Go, constants serve as identifiers for literals
, providing a means to assign meaningful names to values. These constants are restricted to holding values determinable by the compiler during compilation. Unlike some other languages, Go lacks built-in support for declaring values computed at runtime as immutable
. Consequently, there are no immutable arrays, slices, maps, or structs in Go. Furthermore, there's no inherent mechanism to declare a field within a struct as immutable.
Resume
- Constant can only hold values determinable in compile-time
- Numeric Literals
- Boolean
- Strings/Runes
- There are no ways to declare immutable values in runtime
- Arrays, slices, maps, or structs are not immutable
- No mechanism to declare struct fields as immutable
Constants in Go can be typed
or untyped
. An untyped constant behaves like a literal; it has no type but defaults to a specific type
when no other type can be inferred. In contrast, a typed constant can only be assigned directly to a variable of its designated type. The decision to designate a constant as typed or untyped hinges on the rationale behind its declaration. Generally, opting for an untyped constant provides greater flexibility
. However, specific scenarios may necessitate the enforcement of a particular type for a constant
.