0

I have bought hosting from reg.ru. I have compiled my website and put it in a subdomain. When I run my app it runs fine without errors. But when I enter that subdomain my website does not respond. I tried adding .htaccess file but no success. Is it normal to deploy my Golang website to hosting? Or should I buy a server? Can I tell that hosting to serve request of that subdomain from my application?

OriHero
  • 1,168
  • 1
  • 10
  • 24

2 Answers2

10

Golang is a compiled language. The output of your compiled source code is a binary that could be executed directly on Operating System (that is compiled for) without needing any other software. The compiled binary itself contains a webserver consuming http requests. That's the main difference from interpreted languages like PHP is, where a webserver (Apache, Nginx) is needed to translate incoming requests to a protocol that PHP understands. See this answer for more information.

I tried adding .htaccess file but no success

The .htaccess file is the Apache webserver configuration file, which you don't need for running a compiled webserver application.

Is it normal to deploy my Golang website to hosting? Or should I buy a server?

The website hosting providers expect you to run PHP or any other application that understands CGI protocol.

You should proceed similarly as for any other compiled language in order to host your golang webapp: buy a server (VPS) from classic hosting providers or use a public cloud provider.

Another option for you would be to use a managed platform like Google's App Engine is. It requires you to deploy golang source code and it manages everything else for you. It might save you some devops hassle.

zdebra
  • 948
  • 8
  • 22
1

As zdebra mentions in his answer, is is very common for shared hosting plans to support CGI. The net/http/cgi package provides support for this in go. As long as you are using the http server from the standard library, it only requires a minor change to your go program.

http.ListenAndServe(":8080", myServeMux) would become cgi.Serve(myServeMux). http.ListenAndServe(":8080", nil) would become cgi.Serve(nil).

Note that you no longer specify a port, since the port is now configured on the webserver.

There are a few caveats to this:

  • This is slower than the built-in http server, since your program has to start up from scratch for every page load. That said, go starts pretty fast. As long as your application doesn't have a bunch of stuff to do at startup, it's not that bad.
  • You can't run background threads. Your program starts when the request comes in and should die right after the response goes out.
  • Memory and go channels can't be shared between different requests.
9072997
  • 849
  • 7
  • 21