> For the complete documentation index, see [llms.txt](https://book.codewithgo.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.codewithgo.com/program-structure.md).

# 08. Hello, World!

### Go Program Order

* package statement
* imports statement
* functions, variables, constants etc.

{% code title="main.go" %}

```go
package main

import "fmt"

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

{% endcode %}

### Package statement

* each go file must start with package declaration.
* `package` is the keyword used to declare a package.

{% code title="main.go" %}

```go
package main
```

{% endcode %}

* packages are used modularity, encapsulation, separate compilation, and reuse.
* Go comes with lot of in-build library packages

{% embed url="<https://golang.org/pkg/>" %}

* `crypto` - cryptography related code.
* `fmt` - formatted input/output related code.
* `os` - operation system related code.
* `net/http` - http related code.

### Import statement

* import statements are used to import external packages.
* `import` is the keyword used to import a package.

{% code title="main.go" %}

```go
import "fmt"
```

{% endcode %}

* before using functionality provided by any package, it needs to be imported.
* multiple packages can be imported using single import clause.

{% code title="main.go" %}

```go
import (
	"fmt"
	"net/http"
	"os"
)
```

{% endcode %}

* unused imports are not allowed.

### Main package

* declaring a main package is way to telling Go that this is executable and not library.
* Main package should have method named `main` for Go to be able to execute it.
* effectively, main is starting point for application to run from a functionality point of view.

{% code title="main.go" %}

```go
func main() {
	fmt.Println("Hello, Pune!")
}
```

{% endcode %}

### Declaring a function

* function is a code block that can be called by name.
* a function is declared using `func` keyword followed by name of function.
* function name should be followed by round brackets `()`
* every function should have function body. This is defined using opening and closing curly braces `{}`
* ideally, function can accept input in the form of function parameters and return output in form of return values.

{% code title="main.go" %}

```go
func sayHello() {
	fmt.Println("Hello, Pune!")
}
```

{% endcode %}

### Calling a function

* function that are in same package can be called directly by name.
* function is called using function name followed by `()`
* functions in other packages needs to be called using package name followed by dot `.` and followed by function name.

{% code title="main.go" %}

```go
func sayHello() {
	fmt.Println("Hello, Pune!")
}
func main() {
	sayHello()
}
```

{% endcode %}

{% hint style="info" %}
Identify all the function call in code snippet below.
{% endhint %}

{% code title="main.go" %}

```go
package main

import "fmt"

func sayHello() {
	fmt.Println("Hello, Pune!")
}
func main() {
	sayHello()
	fmt.Println("Getting started is so easy")
	fmt.Println("And fun!")
}

```

{% endcode %}

{% embed url="<https://play.golang.org/p/VToCbUOQYat>" %}

{% hint style="warning" %}
Guess the output
{% endhint %}

{% code title="main.go" %}

```go
package main

import "fmt"

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

{% endcode %}

{% hint style="success" %}
Opening Brace must be on same line where function starts.
{% endhint %}
