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

Values

This section demonstrates basic string concatenation, arithmetic operations, and boolean logic. Let's break it down step by step:

// Package declaration - Every Go program starts with a package declaration.
package main

// Importing the "fmt" package - It provides functions for formatting and printing.
import "fmt"

// The main function - It is the entry point of the program.
func main() {
	// Print the concatenated string "go" + "lang"
	fmt.Println("go" + "lang")

	// Print the result of the arithmetic operation 1+1
	fmt.Println("1+1 =", 1+1)

	// Print the result of the arithmetic operation 7.0/3.0
	fmt.Println("7.0/3.0 =", 7.0/3.0)

	// Print the result of the boolean operation true && false (logical AND)
	fmt.Println(true && false)

	// Print the result of the boolean operation true || false (logical OR)
	fmt.Println(true || false)

	// Print the result of the boolean operation !true (logical NOT)
	fmt.Println(!true)
}

Output

golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false

Now, let's go through each part:

  1. Package Declaration:

    package main
    • In Go, every program starts with a package declaration. The main package is a special package that serves as the entry point for the executable programs.

  2. Import Statement:

    import "fmt"
    • This line imports the "fmt" package, which provides functions for formatting and printing text.

  3. Main Function:

    func main() {
    • The main function is the entry point of the program. Execution of the program begins here.

  4. String Concatenation:

    fmt.Println("go" + "lang")
    • This line prints the concatenated string "go" + "lang" using the Println function from the "fmt" package.

  5. Arithmetic Operations:

    fmt.Println("1+1 =", 1+1)
    fmt.Println("7.0/3.0 =", 7.0/3.0)
    • These lines print the results of simple arithmetic operations. The first line prints the sum of 1 and 1, and the second line prints the result of dividing 7.0 by 3.0.

  6. Boolean Logic:

    fmt.Println(true && false)
    fmt.Println(true || false)
    fmt.Println(!true)
    • These lines demonstrate boolean logic. The first line prints the result of the logical AND operation between true and false. The second line prints the result of the logical OR operation. The third line prints the result of the logical NOT operation on true.

  7. Closing Brace:

    }
    • The closing brace marks the end of the main function.

This Go program thus introduces string concatenation, arithmetic operations, and boolean logic using the basic features of the language. It's a simple and concise example to get started with Go programming.

PreviousHello WorldNextVariables

Last updated 1 year ago

Was this helpful?