Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Getting Started with Go Programming Language
Luqman Shaban
Luqman Shaban

Posted on

Getting Started with Go Programming Language

Introduction

Go, a statically typed, high-level programming language developed by Google in 2009, is considered one of the best choices for building back-end applications. This includes web development (APIs), cloud services (like Docker), and many other use cases.Go’s speed also makes it well-suited for complex applications.

In this blog, I will walk you through the process of initializing aGo project and printing out a simple Hello, World statement. To follow along, make sure you haveGo installed in your machine. Let’s start of by creating a simple hello world project.

Open your terminal, create a new folder named helloWorld and initialize Go inside the project.

mkdir helloWorldcd helloWorldgo mod init helloWorld#output#go: creating new go.mod: module helloWorld
Enter fullscreen modeExit fullscreen mode
  1. Let’s create a simple program that prints hello world.— Open the project in your preferred code editor— create a main.go file— Paste the following code inside the file
package mainimport (        "fmt")func main() {        fmt.Println("Hello, World!")        //prints Hello, World}
Enter fullscreen modeExit fullscreen mode

Explanation

1. package main:
This line declares the package name for this code file. In Go, code is organized into packages, which act like folders to group related functionality.
The name main is special and signifies that this package contains the entry point for the program. This means the code within this package will be executed when you run the program.

2. import (“fmt”)

This line imports a package calledfmt. Go has a standard library with various pre-written functionalities. Thefmt package provides functions for formatted printing and input/output operations.
By importingfmt, you gain access to the functions defined within that package. In this case, we'll be using a function fromfmt later in the code.

3. func main() {}

This line defines a function named main. Functions are reusable blocks of code that perform specific tasks.
The main function has a special role in Go programs. It's the starting point where the program's execution begins.
The curly braces {} mark the beginning and end of the function body, where you place the code that the function will execute.

4. fmt.Println(“Hello, World!”)

This line is where the actual action happens. It calls a function namedPrintln from the importedfmt package.
Println takes one argument, which in this case is a string literal "Hello, World!". A string literal is text enclosed in double quotes.
ThePrintln function prints the provided string("Hello, World!") to the console, followed by a newline character.

Conclusion

Congratulations! You’ve successfully created your first Go program and printed “Hello, World!” to the console. This provides a solid foundation for exploring Go’s functionalities further. In the next section, we’ll delve deeper into Go’s basic building blocks, like variables, data types, and control flow statements. By understanding these fundamentals, you’ll be well-equipped to write more complex Go programs.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Founder & CEO at wrenify, I specialize in creating custom software solutions that drive innovation and efficiency for small businesses.
  • Location
    Nairobi, Kenya
  • Education
    Institute of software Technologies
  • Work
    Software developer
  • Joined

More fromLuqman Shaban

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp