Golang Go语言中 Goland 的 scratch file 使用问题

Golang Go语言中 Goland 的 scratch file 使用问题

今天研究了一下 goland 的 scratch file,发现真是一个很好用的功能,但遇到了一个依赖上的问题

scratch file 可以使用 module 的上下文( using context of module ),我选择了当前在开发的 module (采用 go mod ),就遇到了依赖的问题,但 ide 的补全等功能都可以正常使用

go.mod中 module 名不是一个单词时(包含 . / _ 等符号时),编译运行 scratch file 就会找不到依赖

scratch_4.go:3:8: cannot find package "tttt-" in any of:
	/usr/local/Cellar/go/1.14.2_1/libexec/src/tttt- (from $GOROOT)
	/Users/creedowl/go/src/tttt- (from $GOPATH)

只有在 module 名是一个单词的时候才能正常编译运行,但使用项目的 github 地址等作为 module 名是很普遍的操作,也是官方支持的,所以是否有办法在 module 名不为单个单词时解决 scratch file 的依赖问题?

PS 我测试了项目在 go path 内外,都会有这个问题


更多关于Golang Go语言中 Goland 的 scratch file 使用问题的实战教程也可以访问 https://www.itying.com/category-94-b0.html

4 回复

同样困扰

但是没有好的解决方法(可能我不知道), scratch file 用来写一些不引入第三方的东西还可以

我目前的做法就是创建一个项目,然后把所有临时验证逻辑也好等等的代码都写在那个项目里.

更多关于Golang Go语言中 Goland 的 scratch file 使用问题的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


#1 有一个相关 issue 讨论了这个问题

[issue]( https://youtrack.jetbrains.com/issue/GO-9330)

但也没有很好的解决方案。这应该是 go compiler 的历史问题,scratch file 是单个文件,不属于 go module,所以编译时采用传统的 go path,会到 go path 中找依赖,不知道有没有解决方案

现在用 go mod 还是不行么?

关于Goland中scratch file的使用问题,以下是一些专业解答:

Goland的scratch file是一个非常实用的功能,它允许用户快速编写和测试代码片段,而无需在项目中创建正式文件。以下是一些关于如何使用和优化scratch file的建议:

  1. 创建scratch file

    • 可以通过“File”菜单选择“New”下的“Scratch File”来创建。
    • 在弹出的对话框中,为scratch file命名并选择所需的语言(如Go)。
  2. 使用scratch file

    • Scratch file完全可运行、可调试,支持语法高亮、代码补全等功能。
    • 可以在其中编写代码片段,然后复制粘贴到项目中。
    • 适用于快速测试代码、临时存储代码或与其他开发者共享代码片段。
  3. 优化和管理scratch file

    • 最好给scratch file命名以方便查找,并可以将常用的scratch file保存在一个文件夹中。
    • 如果不需要某个scratch file了,可以选择关闭并删除它,以免占用空间。
    • 熟悉与scratch file相关的快捷键可以提高工作效率。
  4. 注意事项

    • Scratch file是临时的,不应被视为项目的一部分,因此不应将其提交到版本控制系统中。
    • 可以根据个人喜好自定义scratch file的设置,如更改默认语言或调整自动保存选项。

希望这些建议能帮助你更好地使用Goland中的scratch file功能。如有其他问题,欢迎继续提问。

回到顶部