Wednesday, 1 May 2019

Go language: Build executable


Use the command ‘go build goFilePath’, to build the executable from go program.

App.go
package main

import "fmt"

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

$ go build App.go 
$ 
$ ls
App     App.go

As you see 'App' file is created after building. If you run build command in windows, it generates App.exe file.


Run the file 'App' to see the output.
$ ./App 
Hello World


Previous                                                 Next                                                 Home

Go: Hello World Application


Open any text editor and create HelloWorld.go file with below content.

HelloWorld.go
package main

func main() {
 println("Hello, World")
}


Execute the command ‘go run HelloWorld.go’.
$ go run HelloWorld.go 
Hello, World

package main
It is used by the Go compiler to determine application entry point.

func main()
Program execution starts from here. ‘func’ keyword is used to define a function. ‘main’ function do not take any arguments.

println("Hello, World")
‘println’ is a built in function in Go, that is used to print given message to console.

Note
a.   Unlike C, C++ and Java, you no need to end a statement by a semi colon.
b.   String in Go, are placed in between double quotes
c.    Strings in Go are Unicode.


Previous                                                 Next                                                 Home