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
In python to remove duplicates we use unordered iterable set() but in Go it is not so straight forward, there is no package to do this i think... So here's an example on how duplicate elements are removed. Algorithm A flag variable is created using make(), flag is a map that has a key that … Continue reading [Go]Remove duplicate elements in array/slice
The string is a physical address, then this is split into an array, which then captures the last element of the array. This last element is the actual mac address, then replace all "-" with ":" with the strings package. For converting cisco mac address format, first remove all "-" then slice the strings up, … Continue reading [Go]Replace characters in string.
This is a simple program that gets the response body of google.com. The response is returned and stored in resp, then ioutil.ReadAll() reads resp.Body. The resp.Body will then be converted into string and output by fmt.Println.
So here is the assignment 3 for this course Go: The Complete Developer's Guide (Golang) by Stephen Grider, this assignment is a self research assignment which is why Stephen branded assignment3 as hard mode, this is an assignment to train students on how to read the Go documentation. It is not overly hard but it … Continue reading [Go]Assignment 3: Open a text file and display the content
This is an assignment 2 about interface from the course Go: The Complete Developer's Guide (Golang) by Stephen Grider, this topic is difficult to understand but Stephen explained it well. Below is the code for the assignment2. So the purpose of interface in golang is to try to reduce the code, while golang is not … Continue reading [Go]Interface assignment 2
9Pointer is a variable that stores the memory address/register of the variable. If you extract the pointer variable without the asterix(*) sign, you will only get the register of the variable. In order to get the value you need to de-reference the pointer variable by using the asterix(*).