/trying-cobra-go

Trying things with Go and Cobra

Primary LanguageGoMIT LicenseMIT

trying-cobra-go

Trying things with Go and Cobra

Personal notes

VSCode extenstion behaviour

The Go extension for VSCode is hardcore. It removes import "fmt" on save if unused. But automatically adds it when doing fmt.Println("Hello, World!").

It removes unused external imports too, but can't automatically add them, though.

It automatically groups imports on save, turning

import "fmt"
import "rsc.io/quote"

into

import (
	"fmt"

	"rsc.io/quote"
)

Exported name

In Go, a function whose name starts with a capital letter can be called by a function not in the same package. This is known in Go as an exported name.

:= operator

In Go, the := operator is a shortcut for declaring and initializing a variable in one line. Go uses the value on the right to determine the variable's type.

message := fmt.Sprintf("Hi, %v. Welcome!", name)

is equivalent to

var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)

Verbs

Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler.

General

%v	the value in a default format
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of the value
%%	a literal percent sign; consumes no value

More: https://golang.org/pkg/fmt/

External module

require example.com/greetings v1.1.0

To use a local version of a module, or an unpublished module:

replace example.com/greetings => ../greetings

Runing go build will turn this into

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000