How to do a literal *int64 in Go?
Go: Literal *int64
In Go, you can create a literal
*int64
value using the&
operator. The&
operator is used to create a pointer to a variable.
Example
package main
import "fmt"
func main() {
var n int64 = 42
ptr := &n
fmt.Println("n:", n)
fmt.Println("ptr:", ptr)
fmt.Println("*ptr:", *ptr)
// Create a literal *int64 value
litPtr := &int64(42)
fmt.Println("litPtr:", litPtr)
fmt.Println("*litPtr:", *litPtr)
}
Explaination
- First define a variable n of type
int64
and initialize it to the value 42. - We then use the
&
operator to create a pointer ptr to the variablen
. - To create a literal
*int64
value, we use the same & operator with the type name int64. This creates a pointer to a new, uninitialized int64 variable, and returns the pointer value. We then assign the pointer value to the variablelitPtr
. - We can then use the * operator to dereference both pointers and print their values.
I hope this example helps you create a literal *int64
value in Go!
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go