How to create basic program in Golang
Hello, World! is the first basic program in any programming language. You can write this first program in Golang by following the steps below.
First, open the Go compiler. In the Go language, programs are saved with the .go extension and are UTF-8 text files.
Now, first add the main package to your program:
package main
Every program must start with a package declaration . In Go, packages are used to organize and reuse code. In Go , there are two types of programs available, executables and libraries. Executables are programs that can be run directly from the terminal. Libraries are program packages that we can reuse in our programs. Here, the main package tells the compiler that the package should be compiled into an executable program, not a shared library. This is the starting point of the program and also contains the main function .
After adding main package import 'fmt' package to your program:
import( "fmt" )
Here, import keyword is used to import packages into the program and fmt package is used to implement formatted Input/Output using functions.
Now write code in main function to print hello world in Go language:
func main() { fmt.Println("!. Hello World .!") }
In the above lines of code, we have:
- func : Used to create functions in Go language.
- main : Is the main function in Go language, contains no parameters, does not return any values and is called when you execute the program.
- Println() : This method is available in fmt package and is used to display the string ' !… Hello World …! '. Here, Println means Print line.
For example:
// Chương trình Go đầu tiên package main import "fmt"; // Hàm chính func main() { fmt.Println("!. Hello World .!") }
Result:
!. Hello World .!
How to run Golang program
To run a Go program, you need a Go compiler. Once you have a Go compiler, first create a program and save your program with the .go extension , for example, first.go . Now we run this first.go file in the go compiler using the following command, i.e.:
$ go run first.go
You should read it
- How to Install Go on Windows
- How to implement a graph data structure in Golang
- 10 programming languages booming today
- The development flow of programming languages, new problems that are not new
- Do you know what programming language is?
- Udemy's top 5 JavaScript courses
- What is the first programming language in the world?
- In the end, big universities realized that Java was a lousy language if used for introductory programming
May be interested
- Anonymous structures and fields in Golanganonymous structs in golang are temporary structures with no names used for one-time purposes, while anonymous fields allow embedding of unnamed fields.
- Recommended fields in Golangin go structs, fields are encouraged to be the same as anonymous fields, the type of the field is the name of the field.
- How to pass an array to a function in Golangto manage this data efficiently, you often need to pass arrays to functions. in this article, we will learn how to pass arrays to functions in golang.
- How to copy one slice into another slice in Golangto copy one slice into another, go provides a built-in function called copy(). this function allows you to copy elements from one slice (source slice) into another slice (destination slice).
- What is the difference between Go and Java?is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
- How to Install Go on Windowsgolang can be easily installed on windows. here is a step-by-step guide to install golang on windows.
- How to Create a Basic Laptop Design in the Solidworks CAD Programhave you ever wondered how 3d models are designed and wanted to try to model yourself? this article will teach you the basic skills needed to model a basic laptop design as well as many other shapes open the solidworks program.
- Recommended methods in Golangin golang structure, recommended methods work the same way as recommended fields.
- Basic C program structurebefore we study the blocks that make up a c program, first look at a sample c program.
- How to create a database in MySQLmysql can be a scary program. all commands must go through the command line interpreter program (command prompt) without any intuitive interface. therefore, the basic knowledge of how to create and manipulate on a database in mysql can save you time and avoid nuisance.