Create your Go module.
- The
go mod
command can help with initializing a Go module. - Use
go help <command>
for more information about a command.
Create and run an executable that prints "Hello, world!" to the terminal.
- Executables are commonly stored in the cmd folder. ("./cmd/foo/main.go")
- Executable packages must be named
main
and have amain
function. - fmt can help print things to the terminal output.
- The
go run
command can execute a package.
Amend your executable to listen for incoming http requests, and route those requests to a handler which writes "Hello, world!" to the response.
- The
http.ServeMux
type can be used to route incoming requests to a handler. - Your handler will need to implement the
http.Handler
interface. http.ListenAndServe
can be used to listen for incoming http requests and direct them to your router.
Read in a name from the request body and use this to customise the response to "Hello, ".
- The request and response types can be defined as structs.
- The
json
package will be useful for dealing with request and response bodies. - By default, the response status will be set to 200, but this can be overwritten using the
WriteHeader
func onResponseWriter
e.g. in the event of an error.
Change the request & response to accept a transcript and return a summary, and return a placeholder summary for now.
Add the go-openai
client as a dependency.
- The
go get
command is used to add dependencies. - The package information can be found at https://pkg.go.dev/github.com/sashabaranov/go-openai
Create a new struct to wrap the openai client. Add a summarise
func that takes in a transcript, constructs and sends a suitable openai chat request, and returns the response. Call this from your handler.
- Given a type
Thing
it is convention to call the 'constructor' funcNewThing
. - It's common in Go to have multiple type definitions in one file, if they are used together.
- You'll need to pass in a token for the openai service - this can be stored as an environment variable and retrieved using
os.Getenv
.
Amend your code to accept multiple transcripts and return multiple summaries.
errgroup
can be used to set up and wait for concurrent requests.