Go Getting Started

Gayathri. B

 Getting Started with Go

  • To begin programming in Go, you'll need the following tools:
  • A text editor – such as Visual Studio Code (VS Code) – for writing your Go code.
  • A compiler – like GCC – to convert your Go code into machine-readable instructions.
There are many editors and compilers available, but for simplicity and convenience, this tutorial will use an Integrated Development Environment (IDE) that combines both editing and compiling features in one place.

Installing Go

  • To install Go, go to the official download page: https://golang.org/dl/
  • Download the appropriate installer for your operating system and follow the setup instructions.
  • After installation, open your terminal or command prompt and run the following command to verify the installation:

Installing an IDE for Go

1.To write and compile Go code, you’ll need an IDE (Integrated Development Environment). An IDE helps you write, edit, debug, and manage your code efficiently.

2.Some popular and free IDEs for Go development include:
  • Visual Studio Code (VS Code)
  • Vim
  • Eclipse
  • Notepad
3.These tools support Go programming and offer helpful features like syntax highlighting and debugging tools.

4.Note: Online or web-based IDEs are also available, but they often have limited features compared to desktop versions.

5.In this tutorial, we’ll use Visual Studio Code (VS Code) — it’s lightweight, powerful, and beginner-friendly.

6.You can download the latest version of VS Code from:
    https://code.visualstudio.com/

Go Quickstart

Let’s create your first Go program!

Step 1: Set up VS Code for Go
  1. Open Visual Studio Code.
  2. Launch the Extensions view:
  • Click the square icon on the sidebar, or\
  • Press Ctrl + Shift + X.
      3.In the search bar, type "Go" and press Enter.
     4.Find the Go extension by the Go Team at Google, then click Install.
     5.After installation, open the Command Palette:
  • Press Ctrl + Shift + P.
     6.Type and select: Go: Install/Update Tools.
     7.In the list that appears, select all tools and click OK.
        VS Code is now ready for Go development!

Step 2: Initialize Your Project
   Open a terminal in VS Code (Ctrl + ) and run the following command:
    go mod init example.com/hello

Step 3: Write Your First Program
  1. Go to File > New File.
  2. Copy and paste the code below:

package main
import "fmt"
func main() {
    fmt.Println("Hello, World!")
}

3.Save the file as helloworld.go (File > Save As).

helloworld.go

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Running Your First Go Program

  1. Open VS Code.
  2. Launch the terminal (you can use Ctrl + ~).
  3. Navigate to the folder where helloworld.go is saved.
  4. Run the following command:
go run .\helloworld.go

Output:

Hello, World!
Congratulations! You've just written and run your first Go program.

Creating an Executable

To compile your program into an executable file, run:

go build .\helloworld.go

This will generate an executable (helloworld.exe on Windows) in the same folder. You can now run the program without using go run.










Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send