More than a month has passed since the previous Go q&A summary. Today, I will summarize my answers about Go in the past month.

According to a rough count, about 18 Go related questions have been answered in more than a month, mainly from the segmentFault and Zhihu platforms. I hope to add more platforms like StackOverflow and Github’s topics of interest later.

Recently I was writing a small tool to help me answer questions from different platforms, and also to facilitate the monthly summary of questions. It’s a little slow. I hope it can be finished by the end of this month.

The body part begins.

How to parse map[string]string data into target struct in Golang?

It has to do with reflexes.

Reflect.kind () returns a struct (struct), but does not determine the type of the field. This can be implemented using Go’s Switch Type query syntax.

One more thing that was not mentioned in the answer.

When implementing a generic map-to-struct approach, it is easy to think of supporting base types, but there are too many possibilities for struct types. How can we be more flexible? In my view, this can be done with hooks, i.e. if a custom type needs to support map-to-struct conversion, it can be done by adding hook methods to the custom type, such as UnmarshalMap. See Encoding /json for details.

Of course, this work has already been done, see the package on Github, Mitchellh/MapStructure. The Hook mentioned above is also supported.

How does Golang gracefully implement error codes?

Go has a philosophy for error handling. I just gave a simple answer to this question, a simple idea, and I defined user level errors and system level errors. A similar question will be asked in the summary above.

When should Golang return error and when should panic?

My suggestion is whether the error has seriously affected the service logic. If it is within the predicted error, we should return the error and record the log without manual intervention to recover. Otherwise, panic is recommended.

For example, when a service is started, it normally needs to be properly initialized to make sure that the components are working properly. If initialization fails, there is no need to proceed further and panic should prompt.

How is Golang Time implemented?

Question title looks quite big, in fact, the main concern is the transformation relationship between several core constants. It is mainly three times, namely Unix time, Wall time and Absolute time. There is a relatively important transformation formula here, which is a little more complicated when you need to consider smooth years.

Not much introduction, specific oneself see answer.

When do we use Pointers in Golang and when do we use normal objects?

In fact, there are two points. First, if the data structure is large, it is recommended to use Pointers, and value copying will not occur. Second, if you need to modify the structure, you must use Pointers. Of course, if the type is a reference, such as chan, slice, map, this problem is not considered.

Why does make(T, args) in Golang return T instead of *T?

Make targets Go reference types, namely chan, slice, and Map, while new targets Pointers to. Why is the reference type make not a return pointer? This sounds a bit similar to the previous problem, of course, because references don’t have value type problems.

In the loop, append map to Map Slice, which contains all data from the last Append

Like the previous point, map is a reference type, and even though Slice assigns multiple map variables via Append, they point internally to the same address.

Which Pointers to reference types in golang are declared without amperes, and which are declared without asterisks in function definition parameters and return value types

Similar to the previous question, look at the answer.

As far as I am concerned, the four questions listed above have similar knowledge points. To put it simply, they are make and new and. Essentially, it’s a question of what’s inside a variable.

Why are there so few internal functions for Slice in GO?

To be honest, I don’t understand why the Go team didn’t provide a standard library for Slice to make it as easy as they did for string. However, even without such a package, slice’s usual additions, deletions, and checks can be implemented, albeit with a bit of a hack. See my q&A on how to do that.

Does golang equivalence compare directly to addresses?

First of all, the equivalence comparison of Go is the value, not the address. Comparison types for variables in Go are built in, and basically all types can be compared, including interface and struct. The two variables that are comparable must be of the same type. One thing to note, though, is that interfaces are indeterminate, so they compare not only values, but also specific types.

After answering this question, I suddenly remembered that I used reflect.deepequal to compare two structures of the same type, which is a waste of resources.

How does Golang forbid the direct construction of an exported type, which must be constructed through the new function?

Other OO language implementations are basically as simple as defining the corresponding private member attributes and controlling the input parameters through constructors.

So how do you implement Go? In fact, very simple, the idea is similar to OO. We just replaced constructors in OO languages with factory methods in Go, and private variables became private member attributes at the Go package level. We simply create the instance by defining the specified exportable factory method.

Introduction, advanced GO language, what good books to recommend?

I recommend several books for the beginner, intermediate and advanced levels. Interested friends, specifically check the answer.

How to evaluate the plugin package added to the Go standard library?

The answer to this question is I learn first, I buy first. I read a couple of medium articles on the use case of the Plugin, which took about three or four hours. The Plugin package makes Go the ability to dynamically load modules and add new functionality without having to recompile the main program. There is some value in that.

However, there are some problems with the Plugin package, which can be used with some limitations. But if we understand it clearly, we can still figure out in what context we should use it. Check out the answers for details.

How does go Build hide global static string variables?

This problem, I did not find what good way, can think of is through encryption and decryption of the way to solve. Of course, in a real project, we could implement it by introducing external services, such as k8S configuration encryption, using ectD configuration management, etc.

What is the difference in detail between the empty loop in Go and the perpetually blocked chan?

Goroutine is preemptive, which is different from the traditional coroutine model. However, it is not completely preemptive. In the case of single core, the CPU still needs to take the initiative to access resources, and the empty dead loop will always occupy the CPU, which causes a serious waste of resources. Chan block will transfer CPU resources to realize concurrent execution. That’s probably the biggest difference between the two.

In addition to the normal chan implementation of blocking, Q&A also introduces some other ways to achieve similar effects. Interested friends can check the answers.

What’s the difference between FMT.Println and direct Println in Golang?

Println is mainly used by Go itself, such as source code, standard libraries, etc. FMT is used by Go developers. Println is not compatible and may not exist at some point in the future, but FMT functions don’t have that problem.

Of course, there are differences in the use and effect of the two, such as println output to standard error, not standard output.

How to read the Golang source code?

This answer was a big project, piecemeal, that took me almost three weeks. What’s the reason?

I put forward an idea of reading the source code, divided into about three steps, roughly respectively, understand the use, familiar with the architecture and analysis of the principle. I was recently thinking about starting to parse the source code myself, so I decided to take a first step and dump the entire Go source code to get a sense of what’s involved and how it’s used. There are more than forty parts involved, so it takes a long time.

The first step is to understand the use of the piece, some parts to understand the use, but some parts just to understand the level. Reading the source code in its entirety is difficult, but probably doable. Plan to start reading system source code when appropriate.

Do I need to go func() when operating on golang database? How is yield different from python asynchronous operations?

The previous Go q&A summary had a similar question. As I understand it, the premise of using Go Func is that there must be tasks that can be executed in parallel, which is an important premise.

Most of the time, people learn to Go is to Go concurrent, the results after learning to find that it is not used, very depressed ah, always feel that something is wrong, should not be like this. Business development in general, and business development on the Web in particular, really doesn’t use many concurrent scenarios. The problem of concurrency performance is that the service framework has already helped us achieve it without any intervention. To really learn concurrency, you can’t just add, delete, change and check it every day.

Summary complete! If there are any mistakes, please correct them. Finally, thanks for reading, the summary seems to be quite boring!