On previous post about threading, there is no worry of data corruption as each fetch and write is to different destination and write to different file respectively. What if the requirement is to append all data fetched from multiple websites and write them into a single file? There will be race condition which the same … Continue reading [python]Threading, acquiring and releasing lock to access shared resource.
This is a revision for myself on threading. To start threading in python a package threading has to be imported. The dictionary for a list of websites, the script is to save the body of the website into a file. For each key in urls, a thread object is created. The thread object takes in … Continue reading [python]Threading
This is a sample code for scraping items from the web, there are some websites that do not allow direct request from script, so if this is the case then need to add headers. To collect the cities and the descriptions in a list of dictionary use the below. Result
This extends the previous post about capturing user's input. The captured input is stored into a file known as text.txt. To write a string to a file ioutil.WriteFile is used, this method requires three arguments: filename in string string byte slice, which is the ascii representation of the string slice The permission, if unsure check … Continue reading [Go]Capture user’s entered sentence and save into a file.
There are several ways to get the input string. The first method is to use bufioNewReader(os.Stdin) to read the stdin, using the reader object get the user input from stdin until a delimiter is input by user, the entire string includes the delimiter enter by user. First method, use bufio to get stdin Second method … Continue reading [Go]Get user’s input string
Open the file and read its contents, if the file does not exist exit the program with exit status 1, log.Fatal() not only logs the problem but also do os.Exit(1)
You cannot change the value of a struct directly to an array, you will need to dereference the pointer and change the value. There are two methods one is to reference the object's index and change the struct value the other is to pass in the memory address of the current object index and use … Continue reading [Go]Change value to a struct in an array