In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use the new features of Go1.16". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
If static resource compilation cannot be packaged into a binary file, there are usually two solutions:
The first is to identify such static resources and whether they need to follow the program.
The second is to consider packaging it into a binary file.
In the second case, Go is not supported in the past, so we will use a variety of fancy open source libraries, such as go-bindata/go-bindata.
But since Go1.16, the Go language itself has officially supported this feature, and today we will quickly learn about and learn about it through this article.
Basic use
Demo code:
Import _ "embed" / / go:embed hello.txt var s string func main () {print (s)}
We first create the hello.txt file in the corresponding directory and write the text "eat fried fish".
The core / / go:embed hello.txt comments are written in the code. The format of the annotation is simple: the go:embed instruction declaration, plus the address of what is read, can support relative and absolute paths.
Output result:
Eat fried fish
After reading the contents of the static file, the value is automatically assigned to the variable s, and successfully output in the main function.
For other basic types, Go embed also supports:
/ / go:embed hello.txt var s string / / go:embed hello.txt var b [] byte / / go:embed hello.txt var f embed.FS func main () {print (s) print (string (b)) data, _: = f.ReadFile ("hello.txt") print (string (data))}
Output result:
Eat fried fish
We have also made multiple embed annotation declarations in a single code file.
And for string, slice, byte, fs and other types of packaging, do not need too much processing, very convenient.
Extended usage
Apart from the basic usage, embed itself also supports a variety of variations on instructions:
/ / go:embed hello1.txt hello2.txt var f embed.FS func main () {data1, _: = f.ReadFile ("hello1.txt") fmt.Println (string (data1)) data2, _: = f.ReadFile ("hello2.txt") fmt.Println (string (data2))}
When specifying go:embed comments, you can read multiple files at a time, and you can also have multiple lines of annotations for a variable:
/ / go:embed hello1.txt / / go:embed hello2.txt var f embed.FS
You can also specify the directory helloworld in the comments, and then read the file accordingly:
/ / go:embed helloworld var f embed.FS func main () {data1, _: = f.ReadFile ("helloworld/hello1.txt") fmt.Println (string (data1)) data2, _: = f.ReadFile ("helloworld/hello2.txt") fmt.Println (string (data2))}
At the same time, since it can support directory reading, it can also support greedy pattern matching:
/ / go:embed helloworld/* var f embed.FS
Some partners may notice that embed.FS can also adjust the interfaces of various file systems, in fact, the essence is that embed.FS implements the io/fs interface.
Read-only attribute
In the FS provided by embed, we can find that they are both open and read-only methods:
Type FS func (f FS) Open (name string) (fs.File, error) func (f FS) ReadDir (name string) ([] fs.DirEntry, error) func (f FS) ReadFile (name string) ([] byte, error)
Based on this, it can also be determined that the contents of the binary files packaged by embed are only allowed to be read and not changed.
More abstractly, the content of embed is determined at compile time, and modification is not allowed at run time, which ensures consistency.
That's all for "how to use the new features of Go1.16". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 265
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.