Skip to content

A Tour of Go 🤣 Learning Go Programming Language

Notifications You must be signed in to change notification settings

huynhsamha/tour-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tour of Golang

A Tour of Golang - First learning Go Programming Language

The tour of Golang

Go Playground - Online Editor

The Go Playground - Available for share

Notes when install and setup Go - IMPORTANTS

On MaxOS/Linux

Installation

Follow the instructions for your platform to install the Go tools: https://golang.org/doc/install#install. It is recommended to use the default installation settings.

On Mac OS X and Linux, by default Go is installed to directory /usr/local/go/, and the GOROOT environment variable is set to /usr/local/go/bin.

Set your GOPATH

Your Go working directory (GOPATH) is where you store your Go code. It can be any path you choose but must be separate from your Go installation directory (GOROOT).

The following instructions describe one way you can set your GOPATH. Refer to the official Go documentation for more details: https://golang.org/doc/code.html.

Mac OS X and Linux

Set the GOPATH environment variable for your workspace:

export GOPATH=$HOME/go

Also set the GOPATH/bin variable, which is used to run compiled Go programs.

export PATH=$PATH:$GOPATH/bin

On Windows

Quickstart

Run single file

go run [path/to/file].go

This command only runs one file with main() function declared. It's not working with other files in the same package.

Build package

go build

This command build all files in package main into build file tour-golang, which is executable, you can run

./tour-golang

Run package at runtime

go run *.go

This command is similar with build package and run the executable file. It will link files .go and run them.

Notes with packages in Golang

Package declaration

In a directory, one package is declared at the same level. If we want to create another package, create sub-folder and declare with new package for subfolder.

Name of package should be name of directory.

Export modules (type, struct, interface, function, variable) from pacakge

Golang uses first character with capitalized (from A-Z) is exported, that means other package can read.

Example in directory packages

package A
// Foo : exported function, should be comment by go-lint
func Foo() {}
func bar() {
	// this is private in package A, but other file in package A can use bar()
}
// Animal : type animal is exported, shoul be comment by go-lint
type Animal struct {
	a string // that is private
	B int    // that is exported, that mean public
}

And in package B

package B

import "github.com/huynhsamha/tour-golang/packages/A"

func todo() {
	A.Foo() // OK

	// A.bar()
	// cannot refer to unexported name A.bar

	// x := A.Animal{"123", 4}
	// implicit assignment of unexported field 'a' in A.Animal literal

	x := A.Animal{B: 123}
	// x.a = "123"
	// x.a undefined (cannot refer to unexported field or method a)

	x.B = 123
}

Golang topics

OOP in Golang

Detail

DEP - Dependency Pakage Management Go

Detail

Best Documents or References

About

A Tour of Go 🤣 Learning Go Programming Language

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published