Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Hello World

Go version

go version

If you use gvm to install Go, you can change Go versions easily.

  1. Check the available versions

    gvm list
    
  2. Swtich version

    gvm use go1.18
    
  3. Install new version

    gvm install go1.17
    
  4. Uninstall a version

    gvm uninstall go1.17
    

Run main.go

package main

import "fmt"

func main() {
    fmt.Println("Hello, World")
}
  1. Run with go run

    go run main.go
    Hello, World
    
  2. Run with compiled file:

    go build main.go
    
    ./main
    Hello, World
    

Function

func greet(language, name string) (string, error) {
	if language == "Spanish" {
		return fmt.Sprintf("Ola, %s", name), nil
	}
	return fmt.Sprintf("Hello, %s", name), nil
}
func main() {
	fmt.Println("Hello, World")
	greetText := greet("Spanish", "Naka")
	fmt.Println(greetText)
}

Other frequently used commands

  1. Format:
    go fmt main.go
    
  2. Validate: report likely mistakes in packages
    go vet main.go
    
  3. Install
    go install <package>
    
  4. Import necessary libraries and remove unnecessary libraries.
    go mod tidy
    

For more details, https://pkg.go.dev/cmd/go

Practices

  1. Methods
  2. Methods are functions
  3. Methods continued
  4. Pointer receivers
  5. Pointers and functions
  6. Methods and pointer indirection
  7. Methods and pointer indirection (2)
  8. Choosing a value or pointer receiver