Go is Open Source Programming language that is statically typed
. Although it is statically typed, It's so fast that it feels like it's an Interpreted language.
So let's start with the basics. As Go is a Compiled language, Its Code has to be converted to an executable/binary file. Go has a compiler that translates the code into a binary file.
If your file's name was helloworld.go then to compile it on the terminal, The command would look like this :
go build helloworld.go
The command would create a "helloworld" file. To execute that, call ./helloworld
in the terminal
./helloworld
There's a run command that both compiles and executes the code. Although the run command will not create a binary file.
For example to run the command on helloworld.go file, It would look like this:
go run helloworld.go
A Go program should have a package declaration. After the package declaration, We have import statement right below it. It looks like:
package main
import "fmt"
Note that the package name has to be enclosed in double quotes
.
The functions in Go are declared with func
keyword and that is followed by name of the function.
Let's see a basic program.
package main
import "fmt"
func main() {
fmt.Println(" Hello World! ")
}
In the above program in function main we have used the fmt
package which helps us call Println to print.
To run the command we use
go run main.go
This would print the message "Hello World!".
In Go language, the main package is a special package which is used with the programs that are executable and this package contains main() function. Go automatically calls the main function and there is no need to explicitly call it.
You can use go doc on the command line for more information about the packages.
go doc fmt
The above command gives us information about fmt package. For more specific information you could use:
go doc fmt.Println
And Lastly, For more information about Go lang ,You can always check the documentation. This is the official site ๐ฏ.
If you are learning Go language and would want to know more, Below are some resources ๐ฅ.
Resources:
- Watch a video course on freecodecamp .
- There's a free course on Codecademy too. You can check it out here.