why can't save these line of code in vscode with golang extension
package app
import (
"fmt"
)
//this is just func
func TestingHello(){
fmt.Println("Hissssss")
}
only the package app stays and remaining part got deleted on save in vscode.
config both editor.formatOnSave and editor.codeActionsOnSave in the settings.json:
"[go]": {
"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
"source.organizeImports": false
},
},
"go.formatTool": "gofmt",
Basically, your formatOnSave is on for go which is causing this problem.
To disable it, go to your Command Palette (Ctrl+Shift+P), type in "configure language specific settings", and look for Go.
You should now see a json file where you can add the following setting to it:
"editor.formatOnSave": false
This is how the json file looks like if you just have on setting modified for go:
{
"window.zoomLevel": 1,
"[go]": {
"editor.formatOnSave": false,
}
}
By default Format Tool is set to "goreturns" in settings.json, change it to "fmt":
{
"go.formatTool": "gofmt"
}
Now you can comment the imports:
import (
"fmt"
// "reflect"
// "math/rand"
)
I had a similar issue that was caused by not having the right case on method names.
In the following code import "fmt" would disappear.
package main
import "fmt"
func main() {
fmt.println("hello world")
}
Solution I should be calling Println NOT println ! Note the uppercase P. Once changed goreturns adds the import instead of removing it.
Ctrl+Shift+P --> Configure Language Specific Settings
"editor.insertSpaces": false,
"editor.formatOnSave": **false,**
"editor.codeActionsOnSave": {
"source.organizeImports": **false**
}
}
}
That seems strange.
I can understand the import disappearing (as in issue 748) because of goreturns (github.com/sqs/goreturns) removing unused import. But that shouldn't apply in your case.
But if almost everything disappears, that means the file fails to be saved, and revert to its original content.
Maybe another process is keeping an handle on that file, preventing the save operation to proceed.
The reason why this happening is because of what you imported, you didn't use it in the program, so Golang deletes unnecessary dependencies.
For me, the changes did not take effect until I re-loaded the window.
For every change, run
Cmd + Shift + P
>Developer: Reload Window
It's a simple answer that I found:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello")
}
`
Make sure P is capital of Println , I spent hours to find out this error.
On VScode just press (CTRL + SHIFT + P ).
Then, Click on "Configure language-specific settings"
and select GO lang.
Just paste code
"[go]": {
"editor.insertSpaces": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
}
Related
This is newbie question. The dependencies seems to be on github, and it's pretty obvious from the import, so why run doesn't work?
Error is: no required module provides package github.com/hashicorp/go-getter
package main
import (
"context"
"fmt"
"os"
// Problem with line below, getting error: no required module provides package
getter "github.com/hashicorp/go-getter"
)
func main() {
client := &getter.Client{
Ctx: context.Background(),
//define the destination to where the directory will be stored. This will create the directory if it doesnt exist
Dst: "/tmp/gogetter",
Dir: true,
//the repository with a subdirectory I would like to clone only
Src: "github.com/hashicorp/terraform/examples/cross-provider",
Mode: getter.ClientModeDir,
//define the type of detectors go getter should use, in this case only github is needed
Detectors: []getter.Detector{
&getter.GitHubDetector{},
},
//provide the getter needed to download the files
Getters: map[string]getter.Getter{
"git": &getter.GitGetter{},
},
}
//download the files
if err := client.Get(); err != nil {
fmt.Fprintf(os.Stderr, "Error getting path %s: %v", client.Src, err)
os.Exit(1)
}
//now you should check your temp directory for the files to see if they exist
}
Create a folder somewhere called getter, then create a file
getter/getter.go:
package main
import (
"fmt"
"github.com/hashicorp/go-getter/v2"
)
func main() {
fmt.Println(getter.ErrUnauthorized)
}
Notice I didn't use a name like you specified, as it's redundant in this case. The package is already called getter [1], so you don't need to specify the same name. Then, run:
go mod init getter
go mod tidy
go build
https://pkg.go.dev/github.com/hashicorp/go-getter/v2
I'm trying to build a Go package with the build flag -buildmode=c-shared. I'm expecting to get two files myfile.so and myfile.h. However, I'm only getting the .so file. Why is this and how can I fix it?
The full command I am running is:
go build -o myfile.so -buildmode=c-shared myfile.go
I found my "instructions" here as I am planning on calling myfile from Python.
This is my Go code:
package main
import (
"C"
"bytes"
"log"
"encoding/json"
"net/http"
)
func call_request(arg1, arg2, arg3 string) {
// simple golang code to submit a http post request
}
func main () {
}
This is a basic summary of my code, without posting my whole code. However, it may be useful to note that running the example in the link above created a .so and .h file.
As #JimB said, the issue was there was not a header file:
Updated code:
package main
import (
"C"
"bytes"
"log"
"encoding/json"
"net/http"
)
//export call_request
func call_request(arg1, arg2, arg3 string) {
// simple golang code to submit a http post request
}
func main () {
}
I am trying to build a web app with two files.
app.go and main.go are both in the same directory.
app.go
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
type App struct {
Router *mux.Router
DB *sql.DB
}
func (a *App) Initialize(username, password, server, port, dbName, cacheAddr, cachePass string){
}
func (a *App) Run(addr string) {
}
main.go
package main
func main() {
a := App{}
// more code here
}
I thought my main.go file would recognize App{} but my editor is complaining that App is undeclared name
Both files are in the same main package but I am not sure what went wrong. Could anyone help me about it? Thank you!
From the comments I assume you run the following command: go run main.go. This will ONLY load code in main.go (and files included with import statements). To tell Go to load all .go files in the current directory, run the following instead:
go run .
Similarly, to tell VSCode to load alll files start it like this:
code .
I saw the similar question here. But I couldn't solve my case.
I am having project initialised with dep and added the first dependency "Echo". Now folder structure looks like this
|--server
| |--server.go
|--vendor
|--main.go
The server.go has the following code
package server
import (
"net/http"
"github.com/labstack/echo"
)
// TestController : Test controller
func TestController(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
and the main.go has
package main
import (
"github.com/labstack/echo"
"github.com/sfkshan/pos/server"
)
func main() {
e := echo.New()
e.GET("/", server.TestController)
e.Logger.Fatal(e.Start(":1323"))
}
Now vscode shows the warning
cannot use server.TestController (type
func("github.com/sfkshan/pos/vendor/github.com/labstack/echo".Context)
error) as type "github.com/labstack/echo".HandlerFunc in argument to
e.GET
I am not sure why is this happening? If I delete the vendor folder folder the error vanishes. But again after running dep ensure (in this case vendor folder gets created which is expected) the error appears again.
I am new to golang and trying to explore the lang with sample hobby
project for that I need to write the below tree like structure.
Its like File system, one Folder will have many Folder and files.
And tree structure goes on until it has no further branch.
[Fol]
[Fol,Fol,Fol] [Fil,Fil,Fil]
My solution to have:
type Fol struct{
slice of Fol
slice of Fil
}
Its taking time for me to design, So any once help is very much appreciated.
Regards,
Vineeth
Finally I used solution provided in below link:
https://stackoverflow.com/a/12659537/430294
Something like this?
Playground link
package main
import "fmt"
type File struct {
Name string
}
type Folder struct {
Name string
Files []File
Folders []Folder
}
func main() {
root := Folder{
Name: "Root",
Files: []File{
{"One"},
{"Two"},
},
Folders: []Folder{
{
Name: "Empty",
},
},
}
fmt.Printf("Root %#v\n", root)
}
Prints
Root main.Folder{Name:"Root", Files:[]main.File{main.File{Name:"One"}, main.File{Name:"Two"}}, Folders:[]main.Folder{main.Folder{Name:"Empty", Files:[]main.File(nil), Folders:[]main.Folder(nil)}}}