Using Go Lang as a tiny server program at localhost , won't execute request

568 views
Skip to first unread message

John Dutcher

unread,
Aug 4, 2022, 3:16:53 PM8/4/22
to golang-nuts
I have a tiny server program in Go language (main2.go) running and 'listening' on port 8080 in a Windows command prompt session. While running I point a browser to http://localhost:8080/main2.go and main2.go executes and places a small html form into the browser window to fill and submit. If the form ACTION has any requested target for some other Go program or HTML element resident in the same Windows folder as main2.go, the element request is ignored .... ONLY 'main2.go' gets triggered to execute instead in response to the POST request 'submitted'.

I have tried all variations I can think of in the POST ACTION URL format to get the POST to run an element in the Windows folder called 'main.go' (instead of main2.go):

ACTION examples:
http://localhost:8080/main.go
localhost:8080/main.go
/main.go
../main.go
./main.go

BUT, if I load the ACTION URL with something like 'https://www.google.com' it brings the home page for Google right into the browser window no problem.

Why can it not run main.go, which again, resides in the same Windows folder as main2.go which is the Go program running as the swerver ?

Martin Schnabel

unread,
Aug 5, 2022, 3:29:47 AM8/5/22
to golan...@googlegroups.com
hi john,

other than php, go programs are compiled to an executable that can then
be run. you can not put go files in a folder and expect that to work.

you probably followed a tutorial to set up a go http server with a
simple route. the reason you get that idea that you can use file names
comes probably from the simple router mux of the http package that does
check only the prefix of the request path for you.

try just http://localhost:8080 and you probably see the same form.

to have one program that includes the code of both files they need to be
part of the same package and must be compiled together.

from your description i gather that you probably ran "go run main2.go"
witch will only compile that one file into a program and run it.

instead you need to either list all the files of your main package
"go run main2.go main.go". or: go run is meant a convenience for mostly
single file programs, so instead most people use "go install" or "go
build" and execute the resulting program.

if both files are compiled into the same program they can have only one
main function. where you need to setup the http server and its routes.

there you can register the http handlers with path prefixes that you can
call from your html form.

hope that helps

if you need more detailed help please include the commands you ran and
a links to the code pasted somewhere; preferable the go playground.
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/8ae6812d-a551-429d-ab6b-5215063a60a2n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/8ae6812d-a551-429d-ab6b-5215063a60a2n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Marcello H

unread,
Aug 5, 2022, 3:49:54 PM8/5/22
to golang-nuts
This is not how it works. it's not like PHP.

your server has to know the route(s) to a handler. That handler will take care of that particular hot.
In that hit you decide what to send back as a web-response.

Op donderdag 4 augustus 2022 om 21:16:53 UTC+2 schreef John Dutcher:
Reply all
Reply to author
Forward
0 new messages