Project Setup
Now for the fun part!
One of the great things about go lang, is that setting up a project can be literally as easy as the commands below:
mkdir demo_lsp && cd demo_lspgo mod init demo_lsp && touch main.goWe don’t need a crazy amount of files to get started, just a main.go file and a go.mod file.
- go.mod
- main.go
For starter we’ll add a simple logger to our main.go file so that we can see the requests and responses that are executed.
package main
import ( "fmt" "log" "os" "path/filepath" "time")
func main() { logger := getLogger(nil) logger.Println("Hey buddy, I started")}
func getLogger(filename *string) *log.Logger { cwd, err := os.Getwd() if err != nil { panic("Could not get the current working directory") } if filename == nil { name := fmt.Sprintf("log_%s.log", time.Now().Format("2006-01-02")) filename = &name } log_path := filepath.Join(cwd, "logs", *filename) // make logs directory if it doesn't exist // check if the directory exists if _, err := os.Stat("logs"); os.IsNotExist(err) { // create the directory err := os.Mkdir("logs", os.ModePerm) if err != nil { panic("Could not create the logs directory") } }
logFile, err := os.OpenFile(log_path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { panic("hey, you didn't give a good file bozzo") }
return log.New(logFile, "[demo_lsp]", log.Ldate|log.Ltime|log.Lshortfile)}We can go ahead and run our project using the command below:
go run main.goWe should see a directory called logs created in the root of our project with a log file inside it.
- go.mod
- main.go
Directorylogs
- log_2024-06-01.log