GoLang debugger chokes on stdin - goland

I have a problem using GoLand's debugger for a piece of code that tries to read from the stdin. For example, the following code:
package main
import (
"io"
"os"
"strings"
)
func main() {
io.Copy(os.Stdout, strings.NewReader("Start typing now...\n"))
io.Copy(os.Stdout, os.Stdin)
}
executes perfectly when I run it from within GoLand - the console window collects the input properly. But when I use the debug command - I can see the my input appearing in the console window, but the enter key will not end the input string, instead the cursor just moves to the next line.
My versions:
GoLand 2018.2.2
Build #GO-182.4129.57, built on August 23, 2018
JRE: 1.8.0_152-release-1248-b8 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

As you are using OSX there is no simple way to get this working.
Assuming you are using Go 1.10 or newer, change the directory to $GOPATH/src/github.com/user/package, then compile your application using go build -gcflags "all=-N -l" github.com/user/package, and then manually start the application in Terminal manually. Once the application runs, go to Run | Attach to Process... and select the application from the list. This will attach the debugger to the running application.
Please note that the compilation step is needed in order to improve the debugging experience but you should not use the resulting binary in production as (almost) all optimizations have been turned off.

Related

Go build with built-in dependencies

I'm very new to system programming.
I have a Go program which uses net/http and starts an http server.
When I build a Windows binary and tried on a target Windows machine, nothing worked except Printfs before it starts the server.
As soon as I installed Go on the target Windows machine, everything started working!
Here is my program:
package main
import (
"fmt"
"log"
"os/exec"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there #%s!", r.URL.Path[1:])
}
func main() {
path, err := exec.LookPath("go")
if err != nil {
log.Fatal("Go is not your fortune :|")
}
fmt.Printf("Go is available at %s\n", path)
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
If go doesn't build all the dependencies with its program then how do I do it? If it does, why it is not working?
Does target systems have to have go installed prior to run any Go programs?
Please help! Thanks.
Your app doesn't work without Go because you call log.Fatal() if it doesn't find the go tool, and log.Fatal() terminates your app:
Fatal is equivalent to Print() followed by a call to os.Exit(1).
Your executable binary will contain everything it needs if you build it from the source you posted either with go build or go install (see What does go build build? for details). Just don't call log.Fatal() and it should work.
And on a side note: you should check and print errors returned by http.ListenAndServe(), e.g.:
panic(http.ListenAndServe(":8080", nil))
Because if this fails, you won't know why it doesn't work (e.g. you already started the app and the port is taken / in use).
No, the target system does not have to have the Go compiler installed for the executable compiled with Go to run. You can compile your program with go install or go build on one Windows computer, copy it to another one where Go is not installed and run it there.
Make sure the program that you compile starts with the package main statement - otherwise only a library file will be compiled.
Make sure to check whether the user that runs your program has rights to listen on the port that the http server uses. The ports 0-1023 - "Well Known Ports" - "can only be used by system (or root) processes or by programs executed by privileged users" (https://support.microsoft.com/en-us/kb/174904), so try to start your program as a privileged user (for example, Administrator) or use a non-privileged port >= 1024 (ex. http.ListenAndServe(":8080", nil) should start the server on a non-privilege port. Take a very simple example - for example from https://golang.org/doc/articles/wiki/ - and see if you can get it to work.

How do I create an executable from Golang that doesn't open a console window when run?

I created an application that I want to run invisibly in the background (no console). How do I do this?
(This is for Windows, tested on Windows 7 Pro 64 bit)
The documentation found online says I can compile with something along the lines of,
go build -ldflags -Hwindowsgui filename.go
But this gives an error: unknown flag -Hwindowsgui
With more recent (1.1?) versions of the compiler, this should work:
go build -ldflags -H=windowsgui filename.go
When I continued searching around I found a note that the official documentation should be updated soon, but in the meantime there are a lot of older-style example answers out there that error.
Using Go Version 1.4.2
go build -ldflags "-H windowsgui"
From the Go docs:
go build [-o output] [-i] [build flags] [packages]
-ldflags 'flag list' arguments to pass on each 5l, 6l, or 8l linker invocation.
go build documentation
ldflags docs
If you don't want to type the long build instructions every time during debugging but still want the console window to disappear, you can add this code at the start of your main function:
package main
import "github.com/gonutz/w32/v2"
func main() {
console := w32.GetConsoleWindow()
if console != 0 {
_, consoleProcID := w32.GetWindowThreadProcessId(console)
if w32.GetCurrentProcessId() == consoleProcID {
w32.ShowWindowAsync(console, w32.SW_HIDE)
}
}
}
Now you can compile with go build. Your program will show the console window for a short moment on start-up and then immediately hide it.

Not able to start Websphere application Server 8.0 in Rational application developer 8

On the console.. I get the following -
Usage: java [-options] class [args...]
(to execute a class)
or java [-jar] [-options] jarfile [args...]
(to execute a jar file)
where options include:
-cp -classpath <directories and zip/jar files separated by ;>
set search path for application classes and resources
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-? -help print this help message
-X print help on non-standard options
-splash:<imagepath> show splash screen with specified image
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
Eventually the server doesn't start and gets stopped after time-out. The Server was working fine. However, I wanted to restart it. I stopped the server completely and tried starting it. Since then I am facing this issue.
You can check the script that is getting executed when you right click and start the server.
Check if there are any unwanted entries in the java options which does not confirm to the java command line.

xinit Clutter application not working

I've built a simple test app with clutter: A stage with two ClutterText actors to display two words. It works OK when I run it from within gnome but running it from the tty (not gnome-terminal or xterm) with xinit my_app_binary I get an error:
failed to create drawable
Unable to initialize Clutter: Unable to select the newly created GLX context
Window manager error: Unable to initialize Clutter
If I run xinit gnome-terminal from the same tty everything works, gnome-terminal shows up in a black screen. That's the same I want to do with my app.
Is there anything I can do to overcome this error?
All the above are tested in Linux Mint 12. After normal boot I switch to a tty (ALT-F1) and stop lightdm (sudo /etc/init.d/lightdm stop).
Thanks!
EDIT: running as root everything works, so the question is: how to run it as a regular user?
Be sure to set the DISPLAY-var - add it in front of your command
DISPLAY=:0.0 /path/to/myapp
Sometimes this is an access-rights problem - the app should be started with the user who started the X-server
su user-started-x -c 'DISPLAY=:0.0 /path/to/myapp'

TCP works in GHCi, buffered until program exit in program compiled with Leksah

I wrote this simple prototype client to send commands to a server I'm developing. It works perfectly running in GHCi, but the compiled version buffers everything typed in until I type in "quit" and the program exits. At that point all the input text gets sent.
What am I doing wrong? And why is it different when compiled?
Update: it does work as expected if compiled with ghc Main.hs. The problem happens when compiled with Leksah via Package -> Build. Anyone know how to get the command line Leksah is using?
System info: OSX 10.6, GHC 7.0.3, network 2.3.0.2
module Main (
main
) where
import System.IO
import Network
main = do
hServer <- connectTo "localhost" (PortNumber 7000)
hSetBuffering hServer NoBuffering
loop hServer
hClose hServer
where loop :: Handle -> IO ()
loop hServer = do
s <- getLine
hPutStrLn hServer s
case s of "quit" -> return ()
otherwise -> loop hServer
Leksah uses "cabal build", older versions "runhaskell Setup build".
Hmm, it seems Leksah wasn't actually building the app when I thought it was. I must have been running old code without the hSetBuffering call. A clean & rebuild has sorted it out. Apologies and thanks to everyone for your time and help.
edit: Found it - in case anyone else gets confused by this, when package->build is clicked, Leksah does not generate a compiled app if it's in debug/ghci mode.

Resources