{"id":597,"date":"2022-04-28T13:26:00","date_gmt":"2022-04-28T13:26:00","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=597"},"modified":"2022-05-27T15:53:03","modified_gmt":"2022-05-27T15:53:03","slug":"learning-go-day-8","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/study-notes\/learning-go-day-8\/","title":{"rendered":"Learning Go – Day 8"},"content":{"rendered":"\n
Featured image by Egon Elbre<\/a><\/p>\n\n\n\n The standard library defines the For example using Implementing the Stringer interface<\/h3>\n
Stringer<\/code> interface that is used to return a string.<\/p>\n
type Stringer interface {\n\tString() string\n}\n<\/code><\/pre>\n
\n
type Person struct {\n\tName string\n}\n\n\/\/ Implementation for Stringer\nfunc (p Person) String() string {\n\treturn "My name is " + p.Name\n}\n\n\/\/ Your type can now be passed to function that accept Stringer based types\nfmt.Print(person1)\n<\/code><\/pre>\n
Launching other command line applications<\/h3>\n
exec.Command()<\/code> you could build the app during a unit-test and run it.<\/p>\n
import (\n\t"os"\n\t"os\/exec"\n)\n\napp := exec.Command("go", "build", "-o", "myapp")\nif err := app.Run(); err != nil {\n\tfmt.Fprintf(os.Stderr, "Building tool myapp failed with error: %s", err)\n\tos.Exit(42)\n}\n<\/code><\/pre>\n
\n
cmd := exec.Command("ls", "-la")\nout, err := cmd.CombinedOutput()\n\/\/ Convert out to a string for processing\noutString = string(out)\n<\/code><\/pre>\n
\n
cmdStdIn, err := cmd.StdinPipe()\n\nio.WriteString(cmdStdIn, "Input to be passed to stdin of the process")\ncmdStdIn.Close()\n\nif err := cmd.Run(); err != nil {\n}\n<\/code><\/pre>\n
\n
cmd := exec.Command("ls", "-la")\ncmd.Stdout = &someOtherIOWriter\ncmd.Run()\n<\/code><\/pre>\n
\n
cmd.Dir = somePath<\/code>.<\/li>\n
cmd.Env<\/code> which is defined as
Env []string<\/code>. Each string is \u201ckey=value\u201d.<\/li>\n
exec.LookPath()<\/code>.<\/li>\n<\/ul>\n
path, err := exec.LookPath("git")\nif err != nil {\n\tlog.Fatal("git need to be installed")\n}\nfmt.Printf("git was found at %s\\n", path)\n<\/code><\/pre>\n
\n
context<\/code> which among many other things can also be used in combination with
exec<\/code> to execute commands with a timeout or to allow to be cancelled etc. See this<\/a> for a general guide on how to use context.<\/li>\n<\/ul>\n
Miscellaneous<\/h3>\n
\n
time<\/code> package and the
time.Time<\/code> type.<\/li>\n
time.Now()<\/code> method.<\/li>\n
error<\/code> type from your functions\/methods and use
fmt.Errorf<\/code> to return a string based error message.<\/li>\n
errors<\/code> package to check for OS errors.<\/li>\n<\/ul>\n
import "errors"\n\nif errors.Is(err, os.ErrNotExist) {\n...\n}\n<\/code><\/pre>\n
\n
ioutil<\/code> has been deprecated<\/a> from Go 1.16. It is replaced with
io<\/code> and
os<\/code><\/li>\n
os.Remove()<\/code>.<\/li>\n
os.Getwd()<\/code>.<\/li>\n
os.Getenv()<\/code>.<\/li>\n
strings.Join(elems []string, sep string) string<\/code>.<\/li>\n
const multiline = `This is\na multiline\nstring in golang\n`\n<\/code><\/pre>\n
\n
import "path\/filepath"\n\nfmt.Println(filepath.Base("test.txt")) \/\/ test.txt\nfmt.Println(filepath.Base("a\/b\/c\/test.txt")) \/\/ test.txt\n<\/code><\/pre>\n
\n
bytes.Buffer<\/code><\/li>\n<\/ul>\n
func makeBytes() []byte {\n\tvar buffer bytes.Buffer\n\n\tbuffer.WriteString("The quick brown fox ")\n\tbuffer.WriteString("jumped over the lazy dog")\n\n\treturn buffer.Bytes()\n}\n\nfmt.Println(makeBytes())\n\/\/ [84 104 101 32 113 117 105 99 107 32 98 114 111 119 ...\nfmt.Println(string(makeBytes()))\n\/\/ The quick brown fox jumped over the lazy dog\n<\/code><\/pre>\n
Reference to external packages<\/h3>\n