同一台服务器上的多个GO项目

in PHP you can have a project in /var/www/htdocs/project1 another in /var/www/htdocs/project2 etc...

but how can i do it in Go?

option 1) 1 executable for every project. but you would need open a different port for every project so project1 would be http://localhost:3000 , project2: http://localhost:3001 but what happens if you have 1000 projects? do you open 1000 ports? i think that isnt a good practice

option 2) 1 executable for all the projects (1 main.go to rule all of them!). you would need to do something like :

func main() {
    http.HandleFunc("/project1/users", projectOneUsers)
    http.HandleFunc("/project2/users", projectTwoUsers)
    http.HandleFunc("/project3/users", projectThreeUsers)
    http.ListenAndServe(":3000", nil)
}

but you would need to recompile all the projects everytime that you change one of them. bad idea

option 3) ???

What is the best way to do it?

thanks

Option 3 could be a load balancer that acts as a reverse proxy. In that case you need not to expose any bound port to the outside. It is sufficient when the load balancer you access each started go app.

  1. There is nothing inherently wrong with opening a thousand ports. There are 65k to choose from, after all (per IP address!). However, running one thousand services on a single host is an operational nightmare. You wouldn't want that big of an interruption every time a kernel update is due, for instance. There's also the matter of resource contention, system tuning, and attack surface.

  2. The Go compiler is smart and quick. It won't compile everything all the time. Don't jump to conclusions.

  3. Long before your software reaches that kind of size (if ever), you will have automation in place that solves this problem for you. Do whatever is most practical for you right now and change infrastructure if and when required.