将PHP转换为Go语言。我应该如何组织我的项目结构?

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

PHP to Go. How should I structure my projects?

问题

我正在学习Go语言,PHP是我最擅长的语言(我的技能池包括HTML、CSS、PHP、JavaScript和SQL)。我从未真正接触过像C、C++这样的大型恐怖语言。我认为Go语言是一个不错的起点。

假设我有以下的结构:

|App
|server.go
----|Controllers
-------|main.go

在PHP中,包含一个文件意味着你可以访问父文件和所有之前已经包含的文件中的内容(根据一些条件,但大部分情况下是这样)。

在Go语言中,如果我在server.go中有以下代码:

package main

import (
	"REST/Controllers"
	"fmt"
)

type test struct {
	Number int
}

var TestVar = test{}

func main() {
	controllers.Log()
}

我能否在Controller/main.go中访问TestVar?我尝试过,但似乎找不到方法。以下代码会抛出一个未定义变量的错误:

main.go

package controllers

import (
	"fmt"
)

func Log() {
	fmt.Printf("%q", TestVar)
}

我唯一的想法是通过一个函数将它传递下去,但如果我想要实际改变TestVar的值怎么办?我对这门语言了解不多,所以对指针之类的东西也不太了解。而且如果我有10个变量,每次都要将这10个变量传递给一个函数,这不会变得太麻烦吗?

记住,我来自PHP,所以几乎所有的麻烦事都被简化了。

谢谢你的帮助。

英文:

I'm learning Go with PHP being my best out of my pool (my pool is a kiddie pool: HTML, CSS, PHP, JavaScript, SQL). I've never actually gotten into the big scary ones like C, C++, etc. I thought Go would be a fair start.

Let's say I have the structure:

|App
|server.go
----|Controllers
-------|main.go

In PHP including one file means you have access to things on the parent file and all previous files that have been included.(depending on a couple of things, but for the most part).

In Go, if I have this in server.go

package main

import (
	"REST/Controllers"
	"fmt"
)

type test struct {
	Number int
}

var TestVar = test{}

func main() {
	controllers.Log()
}

Is it possible to access TestVar in my Controller/main.go? I've tried but I can't seem to find how. The following code throws an undefined var error:

main.go

package controllers

import (
	"fmt"
)

func Log() {
	fmt.Printf("%q", TestVar)
}

My only other idea is to pass it down through a function, but what if I want to actually change a value in TestVar? I'm not that far into the language, so I don't know much about pointers and all of that. And if I have 10 variables, wouldn't passing 10 variables to a function every time become too much of a hassle?

Remember I'm from PHP, so pretty much all of the dirty stuff was sugarcoated for me.

Thanks for the help.

答案1

得分: 2

请先阅读以下材料:

然后看看,比如 thesrc 项目的结构。

尝试一些教程 - 只需在谷歌上搜索 golang+webapp+tutorials

英文:

Please read the following material first:

Then look at how, say, thesrc project is structured.

Try out tutorials — just google for golang+webapp+tutorials.

答案2

得分: 0

为了扩展@SirDarius所说的不要过度组织代码结构。

Go项目通常有一个包含源代码的目录。每个文件就像一个模块,可能会有很多行代码。每个目录都是一个项目,包括子目录!它们是子项目。

按照这个思路,控制器是一个子项目吗?不,它是实际项目的一部分。所以将它放在父目录中。如果你有一些可以在这个包之外重用的代码,可以创建一个新的包或子包(哪个更适合)。

一般来说,简化!不要创建目录来组织相似的功能,而是通过将它们放入一个文件中来进行分组。

在你的情况下,将main.golog.go放在根目录中。除非你想创建一个通用的日志记录包,那么可以创建一个名为log的目录,其中包含main.go。但这将是一个独立的包,因此不需要从另一个或父包获取变量。如果你似乎仍然需要那个变量,可以将其作为配置传递。

英文:

To expand on what @SirDarius said to not over-organize your code structure.

It is customary for Go projects to have one directory with sources. Each file is like a module and can be quite long in terms of lines. Each directory is a project, also sub-directories! They are sub-projects.

Following that thought, is Controllers a sub-project? No, it's part of the actual project. So put that in the parent directory. If you have some code that could be reused outside of this package you make a new package or a sub-package (whichever fits best).

Generally, simplify! Don't create directories to organize similar functionality, but group them by putting them into one file.

In your case, have main.go and log.go in the root directory. Unless you want to create a generalized logging package, then create a directory called log containing main.go. But this would be an independent package so it wouldn't need to get variables from another or parent package. If you seem to need that var anyways, pass it as a configuration.

huangapple
  • 本文由 发表于 2015年10月12日 23:01:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/33084300.html
匿名

发表评论

匿名网友

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

确定