How to create a webserver using go

go

#1

How do i exactly run a webserver using the go-lang in this cloud9 editor? Here is my sample app:

https://ide.c9.io/amanuel2/golangpractice

I just dont understand how to…


#2

Hi,

I’m not experienced in go to be able to comment on any details, but in general, here’s how it should work:

  • You should make your webserver code (written in go) listen to 0.0.0.0:8080
  • Run your script using the go command line within the Cloud9 terminal
  • Open the application url (Click on ‘Share’ near the top right of the IDE, click on the url next to ‘Application’, and click ‘Open’)
  • You should see something if your webserver code is working fine.

I hope this helps. For help regarding go development of the webserver, please post a question on StackOverflow or other go related forums.

Regards,
​Mutahhir


#3

Where is the share button you were talking about?

I explained the issue even more in this stackoverflow question!


#4

Thats exactly what it is saying: {“name”:“name”,“what”:“item”} @mutahhir After typing the url

golangpractice-amanuel2.c9users.io/item/name 

I really need to get this running so i can use cloud9 for go


#5

Hi! Sorry I’m seeing this so late.

I’m running an application with a little Go Web server on Cloud9, and it’s working as expected for me. I just fire the program (and hence the Web server) up by typing “go run” in the terminal.

As the folks on Stack Overflow have pointed out, your problem is with the logic of your program, not Cloud9.

Are you able to run your app locally on your PC while you’re just starting out? If so, you might consider running it in Eclipse with GoClipse or some other environment where you can debug a running Go program and see which line it’s on and what it’s doing. Failing that, you could just debug the old fashioned way by inserting a bunch of log.Printf() statements sprinkled in your code to give you an idea of what’s happening.

Good luck, have fun!


#6

I was actually just working on this and it’s incredibly simple! Here’s what I did,

in your main I have something like

http.HandleFunc("/", homeHandler)       //This sets the router for the home page / to the homeHandler function
err := http.ListenAndServe(":"+os.Getenv(`PORT`), nil) // set listen port
if err != nil {
   log.Fatal("ListenAndServe: ", err)
}

then my homeHandler function is something like this…

func homeHandler(w http.ResponseWriter, r *http.Request) {
	p := Person{UserName: "Tristan"}
	templates.ExecuteTemplate(w, "indexPage", p)
}

This uses some object called Person with a template called “indexPage” and it injects the UserName of Tristan. Your handler will obviously look different but this is just what mine does.


#7

It worked! Thanks a lot guys