Build Failed: import "example.com/mysql" is a program ,not an importable package

I am connecting to database in gcp console by creating new function using golang with sample code with database is created already using existing one But giving error that “Build Failed: import “example.com/module” is a program ,not an importable package” if example.com is removed then it shows error “must contain dot in first path element before slash”

package main

import (
    "fmt"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    fmt.Println("Go MySQL Tutorial")

    // Open up our database connection.
 
    // The database is called testDb
    db, err := sql.Open("mysql", "username:password@tcp(127.0.0.1:3306)/test")

    // if there is an error opening the connection, handle it
    if err != nil {
        panic(err.Error())
    }

    // defer the close till after the main function has finished
    // executing
    defer db.Close()

}

and for go.mod

module example.com/mysql  
go 1.20
require github.com/go-sql-driver/mysql v1.7.1

I want to connect database through the code

Hi @Aurelia, welcome to the forum.

I do not fully understand the context of the problem.

  1. Where is the import directive

    import "example.com/module"

    that triggers the error?

  2. The message “must contain dot in first path element before slash” indicates that an URL is expected. The Go compiler does not consider import paths as URLs. Where does this message come from?

Hi @christophberger
Thank you for your response

  1. example.com/module” is my module name in go.mod file.For module name it showing error. And in main.go file having these packages
    import (
    “fmt”
    “database/sql”
    _ “githubcom/go-sql-driver/mysql”
    )

2.In go.mod file ,module name is expecting the path of the code where code is placed . This message is showing in gcp console. Also tried by pushing code through github workflow by giving module name as github repo link like github.com/myproject/projectname. But same error it is showing

I still do not see a connection between your setup and the error messages. I suspect this is a GCP specific problem.

Do you use some particular GCP service? It seems that this service expects to receive a library package, and not a main package, that it can import into its framework.

I want to connect database mysql in gcp . I’m trying to connect database using mysql library “githubcom/go-sql-driver/mysql” . after deploying in gcp cloud function that error is showing in logs . It is showing error for module name .

Ok, so you are using Cloud Functions. This is why you get the error:

“Build Failed: import “example.com/module” is a program ,not an importable package”

Google Cloud Functions expects a non-main package with an HTTP handler function inside.

Your code, on the other hand, represents a stand-alone executable that cannot run as a cloud function.

I’d fix that part first (that is, turn the app into a cloud function), and then sort out the database part of the problem.

So in code need to add http handler function along with database part? And module name will be? Is there any another way to check database connection other than cloud function through golang ?

I don’t know much about Cloud Functions. But basically, Code Functions, AWS Labs functions, and other function-as-a-service systems are based on the same principle: a function is triggered through an HTTP request.
I’d guess the quickest way to check the DB connection is to quickly set up an HTTP handler (it’s only a few lines) to trigger the function.

Ok…Thank you …I will check