The Golang of the block chain technology in Ding Xue School building a static Web server implementation method

Source: Internet
Author: User

Today Buckle School Blockchain training course mainly to introduce to you about Golang to build a static Web server implementation method, first used the Golang language program apes should know, in the use of Golang development, we do not need such as Iis,apache,nginx, Supported by servers such as Kangle.

Why is it? The reason is that Golang's Net/http package already provides the HTTP client and server implementation scenarios. Online speech are said Golang is not suitable for web development, compared to PHP, Java,. NET, Nodejs and other types of back-end language, the use of Golang to do web development, is really a big project. Let's take a look at a detailed introduction to building a Web server using Golang.

Funcmain () {

http. Handle ("/css/", http. Fileserver (http. Dir ("template")))

http. Handle ("/js/", http. Fileserver (http. Dir ("template")))

http. Listenandserve (": 8080", nil)

}

Directory structure:

Src

|--main

|| -main.go

|--template

|| -css

|| --admin.css

|| -js

|| --admin.js

|| -html

|| --404.html

The result of this operation is: the path of the template cannot be found.

In fact, I am very puzzled, the author can successfully run this demo, how to my here, will not start?

So here's the question:

1. What causes the program to not come up?

2.http. What path does Dir () point to?

So I traced the log, as follows

2018/01/0711:09:28opentemplate/html/404.html:thesystemcannotfindthepathspecified.

The problem was found to be on the path not found. After solving the first problem, then you need to figure out HTTP. which path Dir () is pointing to.

I looked at the official example:

Log. Fatal (http. Listenandserve (": 8080", http. Fileserver (http. Dir ("/usr/share/doc"))))

From the above example HTTP. Dir ("/usr/share/doc") shows that the path points to the absolute path in the Linux system. Then the problem is solved: I just need to put the HTTP. The path to Dir () changes to the relative path of the runtime, or the absolute path is used.

Another example, using HTTP. Stripprefix () Method:

Toserveadirectoryondisk (/tmp) Underanalternateurl

Path (/tmpfiles/), usestripprefixtomodifytherequest

URL ' Spathbeforethefileserverseesit:

http. Handle ("/tmpfiles/", http. Stripprefix ("/tmpfiles/", http. Fileserver (http. Dir ("/tmp"))))

As you can see, Tmpfiles is a subdirectory under the TMP directory.

Now that the problem is solved, change the code and run it again.

Functemplate_dir () string{

template_dir:= "E:\\project\\gotest\\src\\template"

Returntemplate_dir

}

Funcmain () {

http. Handle ("/css/", http. Fileserver (http. Dir (Template_dir ())))

http. Handle ("/js/", http. Fileserver (http. Dir (Template_dir ())))

http. Listenandserve (": 8080", nil)

}

After the compilation runs, enter localhost:8080/css/in the browser to successfully see the Admin.css file in the template/css/directory.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support the ding-ting school.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.