Exercise files for Go fundamentals programming
my-go-app/
|-- cmd/
|-- internal/
|-- pkg/
|-- test/
|-- vendor/
|-- go.mod
|-- README.md
Root Directory The root of your Go application is typically named after your project. It contains the go.mod file.
cmd
Directory
The cmd directory contains the main applications for your project. Each subdirectory inside cmd is named for each executable your project will generate.
pkg
Directory
The pkg directory holds library code that's ok to use by external applications. The code is organized into packages.
internal
Directory
The internal directory is for private application and library code. This code isn't intended to be imported by other applications.
test
Directory
While Go tests typically reside alongside the code they test, some projects may choose to have a separate test directory for system tests or test data.
- Clone the repository.
- Navigate to the directory in the command line.
- Create a new go project:
go mod init ed-go-fundamentals
- Create a
cmd
directory parented to the root directory. - Create an
app
directory parented to thecmd
directory. - Create a
main.go
file in thecmd
directory. Populate the file with the following code:
package main
func main() {
println("Hello, world!")
}
- Navigate to the app directory in the command line.
- Run the application:
go run main.go
- Commit your changese:
git add .
git commit -m "Completed exercise 1.1"
In Go, a package is a fundamental concept that helps organize code and promote reusability. It's a collection of Go source files in the same directory that are compiled together.
- Directory-based: A package corresponds to a single directory in the Go workspace. All Go files in the same directory are part of the same package.
- Declaration: Each Go file in a package starts with the package keyword, followed by the package name. For instance, package math declares that the file belongs to the math package.
package math
- Naming: The package name is usually the same as the last segment of the import path (e.g., the package in src/myapp/utils would be utils). However, it's not a strict requirement.
- Scope: Identifiers (like types, variables, functions) defined in a package are accessible from any file within the same package.
- Executable Packages: If a package is named main, it's an executable package. This means that when you build the package, it will generate an executable file.
- Library Packages: Any package that is not main is a library package. These packages are used to provide reusable code, such as utility functions, types, constants, etc.
You can import other packages into your Go file using the import statement. Imported packages can be standard library packages or custom packages you've created.
- Organize related code into packages for better code management.
- Keep your package's purpose focused; avoid creating "do-it-all" packages.
- Use clear, concise, and descriptive names for your packages.
- Minimize the number of exported identifiers to maintain a clean and usable package interface.
- Packages cannot share a bidirectional relationship.
- Navigate to the project root directory in the command line.
- Add a reference to the external date package:
go get github.com/rickb777/date
. Notice that new entries were added to `go.mod`` in the root directory. - In main.go, add the following function:
func isLeapYear(year int) bool {
return gregorian.IsLeap(year)
}
- Update the
main
function to callisLeapYear
:
func main() {
println("Hello, world!")
println("Is 2020 a leap year?", isLeapYear(2020))
}
- Run the application. Play with it by changing the year and running it.
- Commit your changese:
git add .
git commit -m "Completed exercise 2.6"