Next, open your main.go file and add the following highlighted line to call the variable from greet.go in a fmt.Println() function:

package main

import (
	"fmt"

	"github.com/gopherguides/greet"
)

func main() {
	greet.Hello()

	fmt.Println(greet.Shark)
}
In your text editor, open main.go and add the following code to call the greet package:

package main

import "github.com/gopherguides/greet"

func main() {
	greet.Hello()
}
To see how you can use variables in a package, let’s add a variable definition in your greet.go file:

package greet

import "fmt"

var Shark = "Sammy"

func Hello() {
	fmt.Println("Hello, World!")
}
Finally, let’s also define a type in the greet.go file. You’ll create the type Octopus with name and color fields, and a function that will print out the fields when called:

package greet

import "fmt"

var Shark = "Sammy"

type Octopus struct {
	Name  string
	Color string
}

func (o Octopus) String() string {
	return fmt.Sprintf("The octopus's name is %q and is the color %s.", o.Name, o.Color)
}

func Hello() {
	fmt.Println("Hello, World!")
}
Open your greet.go file in your text editor and add the following code:

package greet

import "fmt"

func Hello() {
	fmt.Println("Hello, World!")
}
Next you declare any other packages you need to use with the import statement. You’re only using one in this file—the fmt package:

import "fmt"
Open main.go to create an instance of that type at the end of the file:

package main

import (
	"fmt"

	"github.com/gopherguides/greet"
)

func main() {
	greet.Hello()

	fmt.Println(greet.Shark)

	oct := greet.Octopus{
		Name:  "Jesse",
		Color: "orange",
	}

	fmt.Println(oct.String())
}

# Output:
# go run main.go
Finally, we can add the first file in our directory. It is considered common practice that the primary or entry point file in a package is named after the name of the directory. In this case, we would create a file called greet.go inside the greet directory:

└── $GOPATH
    └── src
        └── github.com
            └── gopherguides
                └── greet
                    └── greet.go
Now that you have your directory for your new package, you can create the entry point file. Because this is going to be an executable program, it is considered best practice to name the entry point file main.go:

└── $GOPATH
    └── src
        └── github.com
            └── gopherguides
                └── example
                    └── main.go
Let’s break this first file down. The first line of each file needs the name of the package that you are working in. Since you’re in the greet package, you use the package keyword followed by the name of the package:

package greet

Recommend

How To Write Packages in Go Writing and Importing Packages

How To Install Elasticsearch, Logstash, and Kibana (Elastic Stack) on Ubuntu 16.04 Conclusion Want to learn more? Join the DigitalOcean Community! Still looking for an answer?

How To Install Elasticsearch, Logstash, and Kibana (Elastic Stack) on Ubuntu 16.04 Step 4 — Installing and Configuring Filebeat

How To Install Elasticsearch, Logstash, and Kibana (Elastic Stack) on Ubuntu 16.04 Step 3 — Installing and Configuring Logstash

How To Install Elasticsearch, Logstash, and Kibana (Elastic Stack) on Ubuntu 16.04 Step 2 — Installing and Configuring the Kibana Dashboard

How To Install Elasticsearch, Logstash, and Kibana (Elastic Stack) on Ubuntu 16.04 Step 1 — Installing and Configuring Elasticsearch

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Conclusion Want to learn more? Join the DigitalOcean Community! Still looking for an answer?

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 5 — Testing PHP Processing on your Web Server

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 4 — Configuring Nginx to Process PHP Pages

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 3 — Installing PHP

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 2 — Installing MariaDB

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 1 — Installing Nginx How To Find Your Server’s Public IP Address

How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 Step 1 — Installing Nginx

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 18.04 Step 4 – Creating a PHP File to Test Configuration

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) on Ubuntu 18.04 Step 3 – Installing PHP and Configuring Nginx to Use the PHP Processor