You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -108,9 +108,9 @@ The Cobra [user guide](https://github.com/spf13/cobra/blob/master/user_guide.md)
108
108
109
109
To use your command, install and run it: `go install .`
110
110
111
-
Then run the following so that your command line knows where to look for the executable code that Go is generating: `export PATH=$PATH:$(dirname $(go list -f '{{.Target}}' .))`
111
+
To run the code, you need to tell your command line where executable code compiled from go lives. The way to do this is different depending on your operating system, but here's [a guide on the Go website](https://go.dev/doc/install) — look at anything that mentions `go/bin` on your `PATH`.
112
112
113
-
You should now be able to run `go-ls`.
113
+
Once added, you should now be able to run `go-ls`.
114
114
115
115
Now, when you change your code, install and run it: `go install . && go-ls`
116
116
@@ -146,6 +146,26 @@ If you smash through this, here's some fun/tricky extensions:
146
146
- Write some tests for `go-ls`
147
147
- Extend `go-ls` to support some more features of the real `ls` (for example, `ls -m assets`)
148
148
149
+
### go-cat
150
+
151
+
This one we're going to make in a different, so we can see how to use tools to initialise go projects more quickly.
152
+
153
+
We'll use the [cobra-cli](https://github.com/spf13/cobra-cli/blob/main/README.md) to initialise a new project. There's a guide on that page to installing it, but it's likely `go install github.com/spf13/cobra-cli@latest`.
154
+
155
+
Then `cd` to the `cli-files` directory.
156
+
157
+
Make a `go-cat` directory, `cd` into it, and run `go mod init go-cat` ([documentation here](https://pkg.go.dev/cmd/go#hdr-Initialize_new_module_in_current_directory)).
158
+
159
+
Then run `cobra-cli init .`. [This command](https://github.com/spf13/cobra-cli/blob/main/README.md) will create your initial application code for you.
160
+
161
+
Take a look at all the files it has created. See how they differ or are similar to what you did in the `go-ls` example.
162
+
163
+
Let's try it out: `go install . && go-cat`. It will do nothing, but it's a start.
164
+
165
+
Now it's over to you: set up a command that takes a path to a file as an argument, then opens that file and prints it out. You'll need the built-in go functions `os.ReadFile` and `os.Stdout.Write`, as well as more from the `os` package.
166
+
167
+
Bonus task: handle the error if you pass it a directory rather than a file, like cat does.
0 commit comments