Getting Started With Go: Beginner Tutorial

Getting Started With Go: Beginner Tutorial

Create a file named main.go in your project directory.

package main

import "fmt"

func main() {
	fmt.Println("Hellow World Scripties")
}

From the above code, you should be having the following questions.

  1. How do I run this code?
  2. What does package main mean?
  3. What does import fmt mean?
  4. What does func mean?
  5. How is the main.go file organized?

How do I run this code

Open the terminal and browse to your project folder containing main.go file

Execute your main.go program using the following command.

go run main.go

Following are the available commands in Go. In that, we have used run command to execute our hello world program.

    
 build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	env         print Go environment information
	bug         start a bug report
	fix         run go tool fix on packages
	fmt         run gofmt on package sources
	generate    generate Go files by processing source
	get         download and install packages and dependencies
	install     compile and install packages and dependencies
	list        list packages
	run         compile and run Go program
	test        test packages
	tool        run specified go tool
	version     print Go version
	vet         run go tool vet on packages

What does package main mean:

Every go program must start with a package declaration. It is for code organization and reusability.

There are two types of packages

  1. Executable: Can be executed directly through a command line or a click. This is normally used for creating small utilities. Each executable package is meant for some functionality. Once such example is creating a database backup.
  2. Reusable: Reusable packages are helper functions or reusable codes or a library that can be called within other program files.

How do you know you are creating an executable package or reusable package?

If your package declaration starts with main, it means you are creating an executable package. Any name other than main will be a reusable package.

What does import fmt mean?

It imports a package named fmt. This package helps in printing text to the standard output. It is a standard library. You can view the list of standard libraries available from here

func main()

func represent function. The go program execution starts from the main function. This main function will always reside in the main package file.

fmt.Println(“Hellow World Scripties”)  prints the text mentioned inside the quotes to the standard output.

Getting Started With Go: Beginner Tutorial

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap