Could not import denisenkom

Win10 x64

I’ve run the terminal command from working folder get and install denisenkom package from the Go working directory but it return an error under import section. I’m using the simple.go from the package sample

could not import from $GOROOT and $GOPATH

Which command did you run from the terminal?

The error message you show does look like it was from an editor rather than a terminal.

1 Like

Correct the error shows from terminal. Apparently it was not able to reach the post connection line to print the connection was successful.

I run this in the terminal

go get github.com/denisenkom/go-mssqldb
go install github.com/denisenkom/go-mssqldb

Just a thought it is also related to Go version where the author mention

Requires Go 1.8 or above.

I’m using the latest Go Lang windows installer though.

Go 1.8 is very old, a recent installer should give yo a 1.16 or 1.17.

And I’m not sure what you mean by “reach post connection line”.

What was the output of the commands you ran? Did they error? Can you successfully build your program using go build?

Right I’m in latest version go1.17.3

	// create the connection pool
	fmt.Printf("Starting mssql connection pool\n")

	// create the connection string for ms sql
	connString := fmt.Sprintf("server=%s; user id=%s; password=%s; port=%d; database=%s; ",
		server, user, pwd, port, database)

	Db, err = sql.Open("mssql", connString)
	if err != nil {
		fmt.Printf("Error creating connection pool :" + err.Error())

	}

	fmt.Printf("Connected to MSSQL")

The fmt.Printf output does not show in terminal.

go build

Do not return anything. I would take it a success.

When does it not show up? And what do you see instead?

This sample code mark the denisenkom import with squirky line and in the terminal it only display Starting server at port 8080. I’m able to view the index.html and post the form.

package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"

	// import mssql database driver/library
	_ "github.com/denisenkom/go-mssqldb"
)

// requets send by user to server
// response server return to user

// r pointer to request
func upload_json(rw http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(rw, "Post request successful 14")
	// validae url to upload function
	if r.URL.Path != "/upload_json" {
		http.Error(rw, "403 not found", http.StatusNotFound)
		return
	}

	// validate form  data if valid
	if err := r.ParseForm(); err != nil {
		fmt.Fprint(rw, "upload_json() err: %w", err)
		return
	}

	fmt.Fprintf(rw, "Post request successful")

	// get and set form.input[data] value
	data := r.FormValue("data")

	fmt.Fprintf(rw, "Data = %s", data)

}

var Db *sql.DB

func main() {

	var (
		server   = "localhost"
		port     = 1433
		user     = "golang"
		pwd      = "1234abcD"
		database = "Northwind"
	)

	var err error

	// route to handle the default page such as index.html
	fs := http.FileServer(http.Dir("./static"))
	http.Handle("/", fs)

	// route to handle the upload function
	http.HandleFunc("/upload_json", upload_json)

	// Server status notification
	fmt.Printf("Starting server at port 8080\n")
	log.Fatal(http.ListenAndServe(":8080", nil))

	// create the connection pool
	fmt.Printf("Starting mssql connection pool\n")

	// create the connection string for ms sql
	connString := fmt.Sprintf("server=%s; user id=%s; password=%s; port=%d; database=%s; ",
		server, user, pwd, port, database)

	Db, err = sql.Open("mssql", connString)
	if err != nil {
		fmt.Printf("Error creating connection pool :" + err.Error())

	}

	fmt.Printf("Connected to MSSQL")

}

Yes, then it starts the server and halts the current go routine, waiting for the http.ListenAndServe() call to return. Which will never happen unless an irrecoverable error occurs.

You need to initialize your DB connection before you start the HTTP server.

This is still unrelated to your initial screenshot. For that I just assume that your editor is not set up correctly.

Right, after moving the http.ListenAndServe to bottom I’m able to run QueryRowContext function. But it does not resolve the “could not import error”.

Go env command return the below

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\E440\AppData\Local\go-build
set GOENV=C:\Users\E440\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\E440\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\E440\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\E440\AppData\Local\Temp\go-build1413715468=/tmp/go-build -gno-record-gcc-switches

If it builds and you can run it from the terminal, then there is no import error.

The problem you have is probably just in your editor. You have not told us what editor you use, how you start it, how you installed it, or configured it.

Right, the program runs successfully.

I’m using VS Code with standard installation. Similar to Golang installations are all default.