GoLand Run tool window: No output from cgo-using Go code, output appears only in terminal
Problem
Output from C code embedded via cgo in a Go program is visible when running in the terminal, but does not appear in the Run tool window in GoLand. The working directory for output is not consistently updated, resulting in misleading output depending on which Run/Debug configuration is active. Multiple directories with identical module names (e.g. example4) and non-unique go.mod module declarations may cause the wrong executable or working directory to be used, leading to confusion about output location.
Cause
The Run/Debug configuration in GoLand may point to an outdated working directory or executable due to:
- Multiple directories containing files with the same name and non-unique Go module names
- The
go.modfile’s module path not being unique, causing run configurations to reference the wrong location - Configuration does not automatically update the working directory when switching among project folders
Resolution
- Ensure that each project or example directory has a unique module name in its
go.mod(for example,module github.com/example/learning/example4instead ofmodule example4). - In GoLand, verify that the Run/Debug configuration specifies the correct working directory and main package. Remove or reset any outdated configurations referencing old directories.
- Open Run | Edit Configurations.
- Check the Working directory field and set it to the directory containing your main Go file and the correct
go.mod. - Remove any configurations that refer to previously moved or renamed directories.
- If output from C code still does not appear in the Run tool window:
- Enable Emulate terminal in output console in your run configuration (Modify options | Emulate terminal in output console).
- In your C code, add
fflush(stdout);afterprintf(), or set unbuffered mode withsetvbuf(stdout, NULL, _IONBF, 0);.
- Run
go clean -cachein the terminal to clear stale build artifacts:- Run
go clean -cachein the project directory.
- Run
- Restart GoLand to ensure configuration changes are applied.
- If the problem persists, collect IDE logs via Help | Collect Logs and Diagnostic Data and review for working directory and run configuration issues.
Please sign in to leave a comment.