Skip to content

proposal: Support simple code that contain 3rd package in go run rather than need a go.mod #61788

Closed as not planned
@NewUserHa

Description

@NewUserHa

eg: any simple code that's only few lines and a single file like:

# test.go
package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	fmt.Println("123")
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})
	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

go run test.go will get "test.go:7:2: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory; see 'go help modules'", which is technically correct in the current latest version.

But what about adding the default package searching paths including the two folders more: the $goroot and the current folder, to make those single .go files with simple codes that have 1 or 2 3rd party packages could run without any problem? (it currently can run when only the std libs.)

The change affected things may be:
go run test.go # this one currently will only work if only using std libs.
go get "github.com/gin-gonic/gin" # this one currently will complain like "... outside go mod..." and refuse work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions