Raise custom error in command-line parsing - go

I'm using the flag module to parse my flags, but want to have at least one positional argument. How do I show the usage help when not enough positional arguments are present, as I would in python with parser.error?
Currently, I am manually calling os.Exit, but that feels really cumbersome for what should be a simple error:
package main
import "flag"
import "fmt"
import "os"
func main() {
flag.Parse()
if flag.NArg() != 1 {
println("This program needs exactly one argument")
flag.Usage()
os.Exit(2)
}
fmt.Printf("You entered %d characters", len(flag.Args()[0]))
}

To do things like this, I use the log package.
package main
import "flag"
import "fmt"
import "os"
import "log"
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatalln("This program needs exactly one argument")
}
fmt.Printf("You entered %d characters", len(flag.Args()))
}
log.Fatal() and it's sister methods (log.Fatalln(), log.Fatalf() etc) are all helpers that simply do log.Print() and then follow it up with os.exit(1).
Edit -- Adding link
http://golang.org/pkg/log/#Fatalln

Related

modifying imported functions in go

I could override the builtin print() function's behavior by defining another print() in scope, as in https://play.golang.org/p/Y2ly31oXU67
Is it possible in go to alter the behavior on-the-fly of an imported function, say fmt.Println()?
If you want to 'alter' a builtin function, look at the very fine monkey patch utility https://github.com/bouk/monkey (And pay attention to the warnings, it's only really useful in test functions, and I for one reject any prod code that imports that package)
Import a different package with name "fmt" and implement whatever functions you need in that package. Here's an example:
File go.mod:
module test
File main.go
package main
import (
"test/fmt"
)
func main() {
fmt.Println("Hello, playground")
}
File fmt/fmt.go:
package fmt
import (
"fmt"
"log"
)
func Println(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
log.Printf(msg)
}
Run it on the playground.
The code in this answer does not modify the imported function as asked in the question.

Random number issue in golang

This is the code I'm working with:
package main
import "fmt"
import "math/rand"
func main() {
code := rand.Intn(900000)
fmt.Println(code)
}
It always returns 698081. I don't understand, what the is problem?
https://play.golang.org/p/XisNbqCZls
Edit:
I tried rand.Seed
package main
import "fmt"
import "time"
import "math/rand"
func main() {
rand.Seed(time.Now().UnixNano())
code := rand.Intn(900000)
fmt.Println(code)
}
There is no change. Now it always returns 452000
https://play.golang.org/p/E_Wfm5tOdH
https://play.golang.org/p/aVWIN1Eb84
A couple of reasons why you'll see the same result in the playground
Golang playground will cache the results
The time in the playground always starts at the same time to make the playground deterministic.
Last but not least, the rand package default seed is 1 which will make the result deterministic. If you place a rand.Seed(time.Now().UnixNano()) you'll receive different results at each execution. Note that this won't work on the playground for the second reason above.

Change hex to string

I'm going through the Golang tutorial, and I am on this part
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("My favorite number is", rand.Seed)
}
This returns My favorite number is 0xb1c20
I have been reading on https://golang.org/pkg/math/rand/#Seed but I'm still a bit confused as to how have it instead of show the hex show a string
math/rand.Seed is a function; you are printing the function's location in memory. You probably meant to do something like the following:
package main
import (
"fmt"
"math/rand"
)
func main() {
rand.Seed(234) // replace with your seed value, or set the seed based off
// of the current time
fmt.Println("My favorite number is", rand.Int())
}

passing File pointer in Go

Ok, I've been programming in Go for a few days so I could be considered a newbie, but I cannot figure out how to import the definition of the File struct. I want to pass a *File into a func and I can't seem to get File defined. I'm importing "os" and calling os.Create in my main. How do I import the right item or declare the parameter in my func definition to pass a file pointer?
import "os"
func testfunc(fp *File) { ... }
fp := os.Create("myfile")
testfunc(fp)
Your testfunc declaration should look like this:
func testfunc(fp *os.File) { ... }
I was stuck about half an hour on this one.
Anyway, I hope this helps some other people as well.
Give the qualified path to the struc File
import "os"
func testfunc(fp *os.File) { ... }
fp := os.Create("myfile")
testfunc(fp)

Why TrimLeft doesn't work as expected?

I've expected tag to be "account" but it is "ccount". Why is "a" removed?
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimLeft(s, "refs/tags")
fmt.Println(tag)
}
Run
Use TrimPrefix instead of TrimLeft
package main
import "fmt"
import "strings"
func main() {
s := "refs/tags/account"
tag := strings.TrimPrefix(s, "refs/tags/")
fmt.Println(tag)
}
Please notice that following TrimLeft calls will result the same "fghijk
" string:
package main
import (
"fmt"
"strings"
)
func main() {
s := "/abcde/fghijk"
tag := strings.TrimLeft(s, "/abcde")
fmt.Println(tag)
tag = strings.TrimLeft(s, "/edcba")
fmt.Println(tag)
}
So TrimLeft is not the method which fits your needs. I guess it's impossible to use it in the example you've given to get the result you expect.
It is working as documented:
TrimLeft returns a slice of the string s with all leading Unicode
code points contained in cutset removed
Because there's an 'a' in the first argument (the cutset) the leading 'a' in account is removed

Resources