27
loading...
This website collects cookies to deliver better user experience
goprograms
, and open your code editor in it. For this tutorial I will be using VS Code. The folder will be our workspace.helloworld.go
, and paste the following code:package main
import "fmt"
// This is where program execution starts
func main() {
// Prints your name!
var name string = "User" // Replace this with your name
message := fmt.Sprintf("Hello world, %v!", name)
fmt.Println(message)
}
go run helloworld.go
./goprograms on main [!?⇡] via 🐹 v1.16.5 on ☁️ (us-east-1)
❯ go run .\helloworld.go
Hello world, User!
package main
...
main
. Here, the name is unique to the context in which it exists, which means there is only one package with a particular name in a single workspace....
import "fmt"
...
import
. This keyword (keywords are words with a special meaning to the go language, and are reserved for use) means that we are importing other packages into our workspace. Packages have names, and the name of the package we are importing is called fmt
. By doing this we can use the programs under this package in our programs....
func main() {
}
...
func
keyword signifies a function, which is a discrete block of code that gets executed when called. Here, the name of the function is main
, which is a function that gets called first everytime the program runs....
var name string = "User" // Replace this with your name
...
var
keyword. Variables are used to store data. We have given the variable a name, name, and it's type is string
which is another keyword and means text data can be stored in this variable. We store the string User here....
message := fmt.Sprintf("Hello world, %v!", name)
...
:=
.fmt.Sprintf()
function. It returns a string for us to use.%v
....
fmt.Println(message)
...
fmt.Println()
function.