Go Printing in VSCode - go

A bit confused on why the following code does not print anything in vscode using the go extension (and properly installed Go)
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Nothing at all gets outputted.
If I remove the exclamation mark within in the print statement
It prints out Hello World

Maybe this:
package main
import "fmt"
func main() {
fmt.Println("Hello World!");
}
→ go build main.go
→ ./main
Hello World!
Don't forget the semiocolon at the end of go line and instruction package follow by name of package at the beginning of the file.

Related

Why does VS Code display a working function as "undeclared name"?

I made a very simple Go program, showing the current time. The function gets loaded from another source file with the same package (main).
For some reason, VS Code thinks, the function has an undeclared name.
Here's the code:
main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
displayTime()
fmt.Scanf("h")
}
show_time.go:
package main
import (
"fmt"
"time"
)
func displayTime() {
fmt.Println(time.Now())
}
It's in the same directory, and everything compiles right. It's just that red mark that won't go and would disturb me. Also, the red number gets added by 1, everytime I call a extern function..

How to add an import statement in a Go wrapper generated by SWIG

I have a wrapper generated by SWIG for the Go language. In this wrapper, I inserted some Go code that needs the package reflect. Consequently, I need to add the line import "reflect" in this wrapper. Is there an example that illustrates how to do this in SWIG?
I think what you want is in section 23.4.10 Adding additional go code namely the part about
If you need to import other go packages, you can do this with %go_import. For example...
%go_import("fmt", _ "unusedPackage", rp "renamed/package")
%insert(go_wrapper) %{
func foo() {
fmt.Println("Some string:", rp.GetString())
}
// Importing the same package twice is permitted,
// Go code will be generated with only the first instance of the import.
%go_import("fmt")
%insert(go_wrapper) %{
func bar() {
fmt.Println("Hello world!")
}
%}

Go run/build cannot find source files

I am trying to run a simple hello world style program that imports a print function from a separate custom package but Go is unable to find it despite teh correct $GOPATH etc being set.
What is missing that will make teh file be picked up?
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ echo $GOPATH
/home/etherk1ll/Development/GoWorkSpace/
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ pwd
/home/etherk1ll/Development/GoWorkSpace/src/sonarparser
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ ls
jsonparser.go main.go
etherk1ll#ubuntu:~/Development/GoWorkSpace/src/sonarparser$ go run main.go
main.go:5:2: cannot find package "sonarparser/jsonparser" in any of:
/usr/local/go/src/sonarparser/jsonparser (from $GOROOT)
/home/etherk1ll/Development/GoWorkSpace/src/sonarparser/jsonparser (from $GOPATH)
main.go
package main
import (
"fmt"
"jsonparser"
)
func main() {
fmt.Println("Hello world 1")
fmt.Println(jsonparser.HelloTwo)
}
jsonparser.go
package jsonparser
import "fmt"
func HelloTwo() {
fmt.Println("Hello world 2")
}
Because jsonparser.go and main.go are in the same package, Go requires those files to have the same package name. And because you defined the main function for the execution, the package must be "main".
Step 1: So you should rename jsonparser.go's package to main.
// jsonparser.go
package main
import "fmt"
func HelloTwo() {
fmt.Println("Hello world 2")
}
Step 2: You need to update main.go file to correct the import path:
// main.go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world 1")
HelloTwo()
}
Step 3: Then you run the following command (you must include all necessary files in the command)
go run main.go jsonparser.go

hello.go:1:1: illegal character U+0023

I'm trying to run the hello world from golang in this link
But when I run go install, I'm getting this error:
hello.go:1:1: illegal character U+0023
This is my hello.go
package main
import "fmt"
func main() {
fmt.Printf("hello, world")
}
I'm using Mac OS El Captain
What is wrong?
you have '#' in first line of your code which is invalid,
see this test sample code:
# just remove this line
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
this will give this error:
hello.go:1:1: illegal character U+0023 '#'
but if you remove lines containing # it works fine:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
it seems your IDE is not for Go. See:
https://github.com/visualfc/liteide
https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins
http://www.distilnetworks.com/setup-go-golang-ide-for-mac-os-x/
I had the same problem but had no '#' in the source whatsoever. My LOCALE was set correctly. My problem was that I created the source via cut and paste from:
https://medium.com/#singh.shreya8/how-to-install-go-lang-in-ubuntu-14-04-ubuntu-16-04-ubuntu-18-04-linux-19e4c8aad4b8
The problem was solved by deleting and retyping the double quotes in the source. Just for grins I tried a cut and paste from:
https://golang.org/doc/code.html
and had no problems. Go figure.

Call a function from another package in Go

I have two files main.go which is under package main, and another file with some functions in the package called functions.
My question is: How can I call a function from package main?
File 1: main.go (located in MyProj/main.go)
package main
import "fmt"
import "functions" // I dont have problem creating the reference here
func main(){
c:= functions.getValue() // <---- this is I want to do
}
File 2: functions.go (located in MyProj/functions/functions.go)
package functions
func getValue() string{
return "Hello from this another package"
}
You import the package by its import path, and reference all its exported symbols (those starting with a capital letter) through the package name, like so:
import "MyProj/functions"
functions.GetValue()
You should prefix your import in main.go with: MyProj, because, the directory the code resides in is a package name by default in Go whether you're calling it main or not. It will be named as MyProj.
package main just denotes that this file has an executable command which contains func main(). Then, you can run this code as: go run main.go. See here for more info.
You should rename your func getValue() in functions package to func GetValue(), because, only that way the func will be visible to other packages. See here for more info.
File 1: main.go (located in MyProj/main.go)
package main
import (
"fmt"
"MyProj/functions"
)
func main(){
fmt.Println(functions.GetValue())
}
File 2: functions.go (located in MyProj/functions/functions.go)
package functions
// `getValue` should be `GetValue` to be exposed to other packages.
// It should start with a capital letter.
func GetValue() string{
return "Hello from this another package"
}
Export function getValue by making 1st character of function name capital, GetValue
you can write
import(
functions "./functions"
)
func main(){
c:= functions.getValue() <-
}
If you write in gopath write this import functions "MyProj/functions" or if you are working with Docker
In Go packages, all identifiers will be exported to other packages if the first letter of the identifier name starts with an uppercase letter.
=> change getValue() to GetValue()
you need to create a go.mod file in the root directory of your project: go mod init module_name
the name of exposed function should start with capital letter
import(
"module_name/functions"
)
func main(){
functions.SomeFunction()
}

Resources