Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Constants

This Go code demonstrates the use of constants, basic arithmetic operations, and a function from the math package. Let's go through each part of the code:

package main

import (
	"fmt"
	"math"
)

// Declare a constant 's' with type string and initialize it with the value "constant"
const s string = "constant"

func main() {
	// Print the value of the constant 's'
	fmt.Println(s)

	// Declare a constant 'n' and initialize it with the value 500000000
	const n = 500000000

	// Declare a constant 'd' and initialize it with the result of the expression 3e20 / n
	const d = 3e20 / n
	fmt.Println(d)

	// Print the value of 'd' after converting it to int64
	fmt.Println(int64(d))

	// Print the result of the sine function applied to 'n' from the math package
	fmt.Println(math.Sin(n))
}

When you run this Go program, it will output the values of the constants and the result of the mathematical operations to the console.

The output should be something like:

constant
6e+11
600000000000
-0.28470407323754404

Explanation:

  1. const s string = "constant": Declares a constant named s of type string and initializes it with the value "constant". Constants in Go are similar to variables but cannot be reassigned after their initial declaration.

  2. fmt.Println(s): Prints the value of the constant s to the console.

  3. const n = 500000000: Declares a constant named n and initializes it with the value 500000000.

  4. const d = 3e20 / n: Declares a constant named d and initializes it with the result of the expression 3e20 / n. The constant d is a floating-point number.

  5. fmt.Println(d): Prints the value of the constant d to the console.

  6. fmt.Println(int64(d)): Prints the value of d after converting it to an int64. This conversion is necessary because d is a floating-point number, and math.Sin expects an argument of type float64 or float32.

  7. fmt.Println(math.Sin(n)): Prints the result of the sine function applied to the constant n using the Sin function from the math package.

PreviousVariablesNextFor Loop

Last updated 1 year ago

Was this helpful?