Why my project doesn't work?

Hi all,

I’m starting to learning Golang.

Recently I write some code, it doesn’t work.

main.go

package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
	displayTime()
}

show_time.go

package main

import (
	"fmt"
	"time"
)

func displayTime() {
	fmt.Println(time.Now())
}

Anyone knows why my project doesn’t work, please note me.

Hi! You need to initialize a module with go mod init mymodulename where mymodulename is what you want (but with a sense) and having both files in the same directory in which you’ve run the command before. Then run go run . into the directory. So for instance on unix:

  1. mkdir showtime and cd showtime
  2. go mod init showtime
  3. touch main.go show_time.go and write the code
  4. go run .
1 Like