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
  • Code Breakdown
  • Output
  • Running the Program 🏃
  • Building and Running as a Binary ⚙️
  • Conclusion

Was this helpful?

Edit on GitHub

Hello World

We'll delve into a simple yet fundamental Go program that prints "hello world" to the console.

Code Breakdown

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

Output

hello world

Let's break down the code step by step:

  • package main: Every Go program starts with a package declaration. The main package is special; it indicates that this is an executable program rather than a library.

  • import "fmt": This line imports the "fmt" package, which stands for format. It provides functions for formatting input and output, including printing to the console.

  • func main(): The main function is the entry point of every Go program. When the program starts, it's the main function that gets executed.

  • fmt.Println("hello world"): This line uses the Println function from the "fmt" package to print "hello world" followed by a newline to the console.

Running the Program 🏃

To run this program, follow these steps:

  1. Ensure you have Go installed on your system.

  2. Copy the code into a file with a ".go" extension, for example, hello-world.go.

  3. Open a terminal and navigate to the directory containing your file.

  4. Run the command go run hello-world.go.

  5. You should see the output: hello world.

Building and Running as a Binary ⚙️

Alternatively, you can build the program into a binary and run it separately:

  1. In the same directory as your code, run go build hello-world.go. This will create an executable file named hello-world (or hello-world.exe on Windows).

  2. Execute the binary by running ./hello-world (or hello-world.exe on Windows) in the terminal.

Conclusion

Congratulations! 🎉 You've just created and executed your first Go program. This simple "Hello World" example sets the stage for exploring more advanced concepts in Go.

PreviousCode With Go! 🚀NextValues

Last updated 1 year ago

Was this helpful?