package main
import "fmt"
func main() {
/* This is my first sample program. */
fmt.Println("Hello, World!")
}
- The first line of the program package main defines the package name in which this program should lie. It is a must statement as Go programs runs in packages. main package is the starting point to run the program. Each package has a path and name associated with it.
- The next lineimport "fmt" is a preprocessor command which tell the Go compiler to include files lying in package fmt.
- The next line func main() is the main function where program execution begins.
- The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. Comments are also represented using // similar to Java or C++ comments.
- The next line fmt.Println(...) is another function available in Go which causes the message "Hello, World!" to be displayed on the screen. Here fmt package has exported Println method which is used to display message on the screen.
- Notice the capital P of Println method. In Go language, a name is exported if it starts with capital letter. Exported means that a function or variable/constant is accessible to importer of the respective package.
Execute Golang Program
In part 1, we execute Golang program using a command prompt. In this occassion I'll share how to use a plugin that will help you execute it in Notepad++.
After you install Golang installable archieve :
- Open Notepad++
- Select Plugins > Plugin Manager > Show Plugin Manager (Image 1)
- Search for GOnpp
- Install
- Enjoy
Note : If you install Golang in a custom location, you need to add GOROOT and GOBIN in system's environment variablesGOnpp assists you writing Go-programms. It has code completion and function calltips (using gocode, see below) as well as direct interaction with the go command. Currently the following actions are implemented:gocode complete -> ALT+O || gocode calltip -> ALT+Pgo fmt -> ALT+F || go test -> ALT+T || go install -> ALT+I || go run -> ALT+R
Highlight Golang Codes
This is for Notepad++ too, I have a custom highlight xml for golang. You can download it here. Now, to apply it :
- Open Notepad++
- Select Language > Define your language
- Select import > Navigate to downloaded golang.xml
- Save
- You may need to restart Notepad++
- Select Language > Go
- Enjoy
Line Seperator
fmt.Println("Hello, World!") fmt.Println("I am in Go Programming World!")So, basically the codes above will not produce an error. It will works normally.
It is good, we don't need to waste time when there's a dumb error because of ;.
But, on the other side codes below will produces error.
if(goal = 10)
{
fmt.Println("Finished")
}
You have to change it to be like this
if(goal = 10){
fmt.Println("Finished")
}
Why? Because end of each line is matters.Well it depends on your style, but for me this is annoying hahaha...Identifiers
A Golang identifier is a name used to identify a variable, function, or any other user-defined item. Basically, you can use any character except punctuation characters such as @, $, and % within the identifiers. These are some acceptable identifiers :mouse i
_animal source_File
Kingdom myAddress
For details, you can access my source link below (tutorialspoint, it is really good).
Variables
- Define the data type by yourself
var list_variable_names optional_data_type /*This is the format to create variable*/ var c, ch int var f, salary float32 c = 42;
- Let Golang decide the data type (only for primitive data types)
package main import "fmt" func main() { var x float64 = 20.0 y := 42 fmt.Println(x) fmt.Println(y) fmt.Printf("x is of type %T\n", x) fmt.Printf("y is of type %T\n", y) }
And, the result is
20 42 x is of type float64 y is of type int
You can also declare a multiple type variables in 1 line
var number1, number2, text = 10, 20, "multiple type variables"
Source :
- https://www.tutorialspoint.com/go/go_program_structure.htm