This is a creation in Article, where the information may have evolved or changed. for the default routeDynamic resources are registered through HANDLEFUNC.
Static resources through the handle to register the source code as follows
Handle registers the handler for the given pattern//on the defaultservemux.//the documentation for Servemux explains How patterns is Matched.func Handle (pattern string, handler handler) {Defaultservemux.handle (pattern, handler)}
Pattern matched with handler one by one
Static resources are generally registered in a static resource directory under a corresponding URI
Like what
Pwd,_: = os. GETWD () http. Handle ("/", HTTP. Fileserver (http. Dir (filepath. Join (pwd, "Src/view"))))
One thing to note here is the OS. GETWD () Gets the path to the project and then adds the corresponding static resource path, which means that it actually registers an absolute path.
Here the reason to write absolute path is because the write relative path has been error 404 helpless under To view the source code
(because the relative path caused the 404 problem has been resolved because when I used the idea is not in the command line go run the program, so the execution path is not the current directory
The relative path specified in the program is for the execution path, so the static resource file cannot be found.
Cgo_enabled=0 goos=linux goarch=amd64 Go build main.go)
The code that actually accesses the static resource is as follows
Type Dir Stringfunc (d Dir) Open (name string) (File, error) {if filepath. Separator! = '/' && strings. Indexrune (name, filepath. Separator) >= 0 | | Strings. Contains (name, "\x00") {return nil, errors. New ("http:invalid character in File path")}dir: = string (d) if dir = = "" {dir = "."} <span style= "color: #ff0000;" >f, err: = OS. Open (filepath. Join (dir, filepath. Fromslash (path. Clean ("/" +name))) </span>if Err! = Nil {return nil, Err}return F, nil}
Please note that the red callout is really going to access the static resources of the code here OS. The path to open is actually
Dir + passed in name what is this dir? The source of our registered path is as follows
Func (f *filehandler) servehttp (w responsewriter, R *request) {upath: = R.url. Pathif!strings. Hasprefix (Upath, "/") {Upath = "/" + UPATHR. Url. Path = Upath}<span style= "color: #ff0000;" >servefile (W, R, F.root, path. Clean (Upath), True) </span>}
Func Servefile (w responsewriter, R *request, FS FileSystem, name string, redirect bool) {//......<span style= "color: #ff 0000; " >f, err: = fs. Open (name) </span>//...}
F.root is the previous Http.dir and HTTP. Dir implements the FileSystem open method so it can also be passed in as a filesystem type
Dir is the previously registered static resource directory servefile name and corresponding URI red callout fs.open (name) passed in is the user access URI path
In fact, the open method of calling Dir accesses the static resource file in the Open method of Dir through the open in the OS package.
But this OS. Open access rules I'm still not clear, for example, I write/view is relative Main.go directory is peer, but os.open just can't find
So very helpless only through the OS. GETWD get an absolute path to solve this problem
If there is any prawn to guide the OS. Open for the relative path is how to handle the hope to solve
Here are some basic usage of some standard libraries that I have encountered when I look at the source code.
Path
Path. Clean (name string) string
The method returns the shortest valid path name
/say--->/say
1///say--->/say
2./say---> Say
3 view/. /say---> Say
4 ""----> "."
FilePath
FilePath. Fromslash ()
This method replaces all/replace with the system's split line
FilePath. Join (em...string)
Connect multiple paths together
Like what
/say/view--->/say/view
/say View--->/say/view
Os. Args[0] Gets the path name of the program execution
Os. GETWD () Gets the path of the current project