GoLang debugger chokes on stdin

Viewed 656

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
1 Answers

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