给外部文件访问对象树

huangapple go评论93阅读模式
英文:

Giving access to the object tree to external files

问题

假设我在模块中有以下结构的.go文件:

  • tree_definition.go
  • tree_creation.go

tree_creation.go解析某个文件,并使用在tree_definition中定义的数据结构、方法和函数创建一个对象树(简称otree)。
在解析过程中,我看到了一个名为external.go的文件,其中包含一些代码,旨在访问otree并对其进行修改。
解析完成后,external.go中的代码应该能够通过我在tree_definition中定义的API使用otree并对其进行修改。这一切都发生在运行时。

为了澄清一些事情:

  • 是的,我正在尝试复制DOM的行为。
  • external.go来自另一个目录,而不是模块内部。
  • 只有在tree_creation将external.go添加到otree的数据结构中时,external.go才可见。
  • 我已经完成了几乎所有复制的步骤。这是最后一部分。不幸的是,我暂时无法提供Git链接,让你自己查看,但我会感激任何可以帮助我澄清情况的评论。

最好的问候!
我想我有一个想法。我稍后会发布它。

英文:

Suppose I have a following structure of .go files inside the module:<br/>
<ul>
<li>tree_definition.go</li>
<li>tree_creation.go</li>
</ul><br/>
The tree_creation parses some file and creates and object tree from it (otree for short) using data structures and methods (as well as functions) defined in tree_definition.<br/>
During the parsing I see external.go file that contains some code, that aims to access otree and modify it.<br/>
After the parsiing is complete, the code in external.go should be able to use otree through the API I defined in tree_definition and modify it. It all happens during the runtime.<br/><br/>
For clarifications:<br/>
<ul>
<li>Yes, I'm trying to replicate the behavior of the DOM</li>
<li>The external.go comes from another directory, not from inside the module</li>
<li>The external.go is visible only during the runtime when tree_creation adds external.go mention to the data structure of otree</li>
</ul><br/>
I've completed almost all the steps to replicate it. This is the last part. Sadly, I cannot provide the link to the Git at the moment so you could see it yourself, but I would appreciate every comment that can help me clarify the situation.<br/><br/>
Best regards!<br/><br/>
I think I get an idea. I'll post it later.

答案1

得分: 0

好的,下面是翻译好的内容:

好的,我们开始吧!
最终,在我的朋友 Bing Chat 的帮助下,我找到了一个简单的解决方案,而不需要使用内存文件或通过本地主机的HTTP请求。这个解决方案非常优雅,对我来说完美地工作!
我仍然认为我会使用内存文件,但我们会看情况。这取决于可扩展性。
此外,特别感谢 @BurakSerdar 提供的答案。

package main

// 这部分将在运行时文件中
import (
	"bufio"
	"fmt"
	"os/exec"
)

func main() {
	cmd := exec.Command("go", "run", "user_defined.go")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	err = cmd.Start()
	if err != nil {
		panic(err)
	}
	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
		if scanner.Text() == "some_command" {
			fmt.Println("found some_command")
		}
		if scanner.Text() == "another_command" {
			fmt.Println("found another_command")
		}
	}
	err = cmd.Wait()
	if err != nil {
		panic(err)
	}
}

编辑 1
结果发现有三种进程间通信的方式,但对于我的问题来说,最适合的是共享内存。

英文:

Okay, here we go!<br/>
Finally, with the help of my pal, Bing Chat, I was able to find simple solution without using memory files or HTTP requests via localhost. Presented solution is elegant and works perfectly for me!<br/>
I still think I'll use the memory file, but we'll see. It depends on scalability.<br/><Br/>
Also, special thanks to

> @BurakSerdar

for his answer.

package main

// This will be in the runtime file
import (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os/exec&quot;
)

func main() {
	cmd := exec.Command(&quot;go&quot;, &quot;run&quot;, &quot;user_defined.go&quot;)
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	err = cmd.Start()
	if err != nil {
		panic(err)
	}
	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
		if scanner.Text() == &quot;some_command&quot; {
			fmt.Println(&quot;found some_command&quot;)
		}
		if scanner.Text() == &quot;another_command&quot; {
			fmt.Println(&quot;found another_command&quot;)
		}
	}
	err = cmd.Wait()
	if err != nil {
		panic(err)
	}
}

<br/>
EDIT 1<Br/>
Turns out there is 3 ways of communicating between processes, but the best suited for my problem - Shared Memory.

huangapple
  • 本文由 发表于 2023年4月8日 12:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75963259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定