In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of what the role of forgery is in the Go language version, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Go language version of what the role of forgery is. Let's take a look.
Friends who have used the Python language may have used forgery_py, which is a tool for falsifying data. Can falsify some commonly used data. It is very useful in our development process and effect presentation. But there is no Go language version, so let's do it.
Start with the source code
There is an example code in the PyPi of forgery_py:
> import forgery_py > forgery_py.address.street_address () upright 4358 Shopko Junction' > forgery_py.basic.hex_color () '3F0A59' > forgery_py.currency.description () u'Slovenia Tolars' > forgery_py.date.date () datetime.date (2012, 7) 27) > forgery_py.internet.email_address () upright brianling zazio.mil' > forgery_py.lorem_ipsum.title () u'Pretium nam rhoncus classicesystems > forgery_py.name.full_name () u'Mary Peters' > forgery_py.personal.language () upright briantry
From the above method calls, we can see that there are a series of * .py files under forgery_py, in which there are various methods to achieve various functions. Let's take a look at its implementation principle by analyzing the source code of the Python version of forgery_py.
# the first-level directory of # ForgeryPy package ├── dictionaries # forges the content and source directory, and the directory stores some text files ├── dictionaries_loader.py # load file script ├── forgery # main directory to achieve various data forgery functions. All the python files stored in the directory are ├── _ _ init__.py.
Let's take a look at the scripts in the forgery directory.
$cat name.pyimport randomfrom.. dictionaries_loader import get_dictionary__all__ = ['first_name',' last_name', 'full_name',' male_first_name', 'female_first_name',' company_name', 'job_title',' job_title_suffix', 'title',' suffix', 'location' 'industry'] def first_name (): "Random male of female first name."_ dict = get_dictionary (' male_first_names') _ dict + = get_dictionary ('female_first_names') return random.choice (_ dict). Strip ()
_ _ all__ sets the method that can be called.
The first_name () method is a typical method of falsifying data in forgery_py, and we only need to analyze it to know how forgery_py works.
With very little code in this method, you can easily see the merging of the data obtained by _ dict = get_dictionary ('male_first_names') and _ dict + = get_dictionary (' female_first_names'), returning random data at the end of return random.choice (_ dict). Strip (). Its focus is on get_dictionary (), so we need to look at its location, the dictionaries_loader.py file.
Cat dictionaries_loaderimport randomDICTIONARIES_PATH = abspath (join (dirname (_ _ file__), 'dictionaries')) dictionaries_cache = {} def get_dictionary (dict_name): "Load a dictionary file ``dict_ name`` (if it's not cached) and return its contents as an array of strings." Global dictionaries_cache if dict_name not in dictionaries_cache: try: dictionary_file = codecs.open (join (DICTIONARIES_PATH, dict_name), 'rushing,' utf-8') except IOError: None else: dictionaries_ cache[ dict _ name] = dictionary_file.readlines () dictionary_file.close () return dictionaries_ cache[ name]
The above is the content of the dictionaries_loader.py file after the comments are removed. Its main implementation is to define a global dictionary parameter dictionaries_cache as the cache, and then define the method get_dictionary () to obtain the source data. In get_dictionary (), every time a method under the forgery directory is called, the cache is checked first, and the data in the cache dictionary is output directly. There is no corresponding file under the read dictionaries and stored in the cache. Finally, the data is returned.
Generally speaking, the principle of forgery_py is: a method call to read the cache in memory, the existence of the cache will be returned directly, and if there is no existence, it will be read and written to the corresponding text file and returned. The returned data is then randomly selected for output.
Using Goto language to implement
After understanding how forgery_py works, we can use the go language to implement it.
# forgery basic directory $cat forgery ├── dictionaries # data source │ ├── male_first_names ├── name.go # specific functions to achieve └── loader.go # load data
We will also create the corresponding directory according to the python version.
A cache that implements the reading of data:
/ / forgery/loader.gopackage forgeryimport ("os"io"bufio"math/rand"time"strings") / / Global cache mapvar dictionaries map [string] [] string = make (mapping [string] [] string) / / randomly outputs func random (slice [] string) string {rand.Seed (time.Now (). UnixNano () n: = rand.Intn (len (slice) return strings. TrimSpace (slice [n])} / / the main data loading method func loader (name string) (slice [] string Err error) {slice, ok: = dictionaries [name] / / data exists in the cache Directly return if ok {return slice, nil} / / read the corresponding file file, err: = os.Open (". / dictionaries/" + name) if err! = nil {return slice, err} defer file.Close () rd: = bufio.NewReader (file) for {line, err: = rd.ReadString ('\ n') slice = append (slice Line) if err! = nil | | io.EOF = = err {break}} dictionaries [name] = slice return slice, nil} / / Unified error handling func checkErr (err error) (string, error) {return ", err}
Implement specific functions:
/ / forgery/name.go// Random male of female first name.func FirstName () (string, error) {slice, err: = loader ("male_first_names") checkErr (err) slice1, err: = loader ("female_first_names") checkErr (err) slice = append (slice, slice1...) Return random (slice), nil}
This implements the python language version of forgery_py using Go.
This is the end of the article on "what is the role of forgery in the Go language version?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what is the role of forgery in the Go language version". If you want to learn more, you are welcome to follow the industry information channel.
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: 0
*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.