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
- set up a command that takes an argument, opens the file and prints it out (`os.ReadFile`, `os.Stdout.Write`)
158
-
- handle the error if you pass a directory like cat does
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