GO

 @ALL NOTE OR PROGRAM ARE COLLECTED FROM SOLOLEARN


Welcome to our Go course!

Go is an open source programming language that makes it easy to build simplereliable, and efficient software.

Go was designed at Google in 2007 to improve programming productivity in an era of multicore, networked machines and large codebases.

The language is often referred to as Golang because of its domain name, golang.org, but its proper name is Go.

Welcome to Go

Go was designed at:

Google
Apple
Microsoft
Oracle

Hello, World!



Let's start with a simple program that outputs "Hello, World!" to the screen.
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
GO
This might seem like a lot of code for just a simple output, but don't worry - - we will break it down and explain each of the lines of code and what each statement is responsible for.
Run the code to see the resulting output.

Packages



Every Go program is made up of packages.

A Go program starts running in the main package.

This is why we need to declare our code as the main package -- to make it run and create the output:
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
GO
The first line of the code defines that this is the main package.

Packages

Which package is used when you run a Go program?

rand
main
package
math

Imports



Apart from main, Go has many packages that can be imported and used in the code to accomplish different tasks.

One of the most popular packages is "fmt", which stands for format, and provides input and output functionality.

To import a package, we use the 
import statement:
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
GO
You can import multiple packages at once, using parentheses. For example:
import (
"fmt"
"math"
)

Imports

Fill in the blanks to import the math package:

"
"


Imports



Each package has exported names, which you can access after importing them.

In Go, a name is exported if it begins with a capital letter.
You can access the exported names using the package name, a dot, and the exported name.

In our case, we access the Println() function of the "fmt" package, which is used to generate the output:
package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
GO
The Println() is an exported name, which is why it starts with a capital letter.
We provide the output we want to generate inside the parentheses enclosed in quotes.

Imports



Similar to other programming languages such as Java or C++, func main() is the entry point of our program. It is the function that gets executed when we run our program.

Here is our code which generates a text output:
package main

import "fmt"

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

GO
You can see that the code:
1. defines the main package
2. imports the "fmt" package used for output
3. defines the main() function.
4. Uses the Println() function of the fmt package to generate the desired output.
We will learn more about functions in the coming modules.


Comments



Now, that we know how to create a simple Go program and output text, let's learn about comments!
Comments are statements in the code that explain what the code is doing.

In Go, you can create a single line comment using two slashes //.

For example:
// generate the output
fmt.Println("some text")
GO
Anything that follows the slashes is considered a comment.
Writing comments is a good practice and allows you and other developers to easier read and understand what the code is doing.

Comments

Fill in the blank to create a valid comment.

output is a number

fmt.Println(42)

Comments



You can also create multi-line comments in Go!
To do this we put the comment inside /* and */.

For example:
func main() {
/*
This is an example program
that only generates a simple
text output using the "fmt" package
*/
fmt.Println("Hey")
}
GO
As comments are not executed as code, you can use /* */ to comment out code blocks that you do not want to execute while you are testing your program.

No comments:

Post a Comment