SEO is a thief interesting thing Golang into the pit series

Source: Internet
Author: User
Tags coding standards
This is a creation in Article, where the information may have evolved or changed.

These two days hooked on SEO. Really look down on Baidu's bid rankings, but as a commercial website, making money is a matter of understandable. Only the work of Lei Feng, no big gold Lord is not long. After finishing the homework, found that Baidu and Google's SEO strategy is not the same, almost can not be common. Baidu currently occupies 70% of the domestic search market share, although despised Baidu's dirty move, but also can only sink the heart to ponder over Baidu's SEO strategy. Previously did not contact the SEO, do not understand the water inside. These two days with a fan to study SEO, only to find the original content is original, Web type (static/dynamic), keyword coverage, robots whether to meet the requirements, the quality of the chain, the site structure will affect the final ranking. Of course, it's all about the technical operation on the materializing, not mentioning those gray industries. Since it is called the grey industry, it is difficult to understand how it works without the old driver. Asked a number of SEO optimized friends learned that some SEO optimization immediately, the basic after a week can be rushed to the top 10, but do not renew, immediately down (there must be a fishy, but the layman still is not understand). Some SEO will promise to keep how long, the results can be imagined, this ranking will gradually gradually decline. Previously felt write code is a technical work, recently more and more found that writing code is really a simple life, these are unclear, and can earn money is the technical work. These days have time, and then look at SEO, see if you can put their own blog also rushed to the top 10.

But before the rush to the top ten, insist on the series finished.

< Korean chubby brother > that section, chatted a mouth, converting from a normal type to interface{} is an implicit conversion. This section is about interface{} This type of bar, or the total feeling of less than a piece of content. It's an entry-level book anyway, it's not about talking. When I'm ready to finish, a video series will be called <golang talk show >. If you have good jokes, good ideas or to promote things, welcome to email me (ztao8607@gmail.com), now there is a film and television company has made up, other people seize oh.

The interface{} in Golang has two meanings, and the first layer is pure interface{}, which can be understood as the object base class of Java. So all data types can be converted to interface{}. Another layer of meaning is the interface class. This interface class and Java interface classes are almost the same, are only a few methods are declared, but not specifically implemented. In fact, the first layer inside the interface{} can be understood as a special interface class, this interface class does not declare any method. So two layers of meaning can be unified into one thing.

If you directly put Java inside the meaning of the interface to take over, this is no meaning, also do not have to write Golang inside the interface class. Golang interface and Java interface are essentially the same, but the appearance of the generations. Let's see how to declare an interface in Golang:

type interface_name interface {   method_name1 [return_type]   method_name2 [return_type]   method_name3 [return_type]   ...   method_namen [return_type]}

When declaring a struct, it is the type name struct. Here the Declaration interface, which is with the type name interface, so does not have to be confused. Within a struct, all member properties are, and in interface, they are all member functions. From this point of view, Golang and Java interface Statements differ little, the big difference in how to use the above, Golang use is much more flexible than Java. To speak with facts, look at the following:

type Shape interface {   area() float64}func getArea(shape Shape) float64 {   return shape.area()}

Declares a shape's interface, which has a member function, an area, and returns a float64. Getarea only accepts the shape interface type. Well, it's time to implement the class.

type Circle struct {   x,y,radius float64}func(circle Circle) area() float64 {   return math.Pi * circle.radius * circle.radius}

Now the Circle class implements the interface for just shape. Do not believe, you hit a code, verify:

circle := Circle{x:0,y:0,radius:5}fmt.Printf("Circle area: %f\n",getArea(circle))

Hey, eaten can give you the output.

If there is only one implementation class, it will not show the advantages of the interface. Then the previous implementation class:

type Rectangle struct {   width, height float64}func(rect Rectangle) area() float64 {   return rect.width * rect.height}rectangle := Rectangle {width:10, height:5}fmt.Printf("Rectangle area: %f\n",getArea(rectangle))

Look, it's not the same. The Shape interface class is now implemented in both rectangle and circle. How did it come true? If you are from Java to turn around, will be the default to find extend keyword, see, above can not extend keyword.

The secret of implementation is that the rectangle and circle two classes have an area () float64 function. So in Golang, how to implement an interface class? That is, the functions in the interface class are implemented, it becomes.

No matter how many functions are defined in the interface class, they are implemented. If not, write a function declaration with the empty implementation on it. According to this logic, would a realization class theoretically be able to implement an infinite number of interfaces? Well, yes, you are right! That is true!

If there's too much code in a project, or a couple of hands, and when you're parsing the code, you don't know how many interfaces this class implements. With the so-called pros and cons, the flexibility of the interface in Golang can greatly reduce the amount of code and reduce coupling. But it also reduces readability, for example, if the program is a bug, and the bug is exactly in an interface function, when looking at the code, you do not know which implementation class is the problem. Not to mention the use of the Go Debug tool, there are very few online debugging through the debug point of opportunity to debug, an online environment so easy to debug, it is too insecure. In theory, the code in the production environment should not contain debug information. Secondly, some problems only meet certain conditions, such as large flow, high concurrency, some special requests can be reproduced, rushed to use debug, has destroyed the replication environment, half a day can not find the problem. Therefore, it is recommended to write code, it is important to remember the output log, it is best to output the most detailed log in the key node.

The basic use of interfaces in Golang is these, if you have a Java foundation then there is no learning difficulty. If not, it doesn't matter, write a few more code is almost. Here's a look at the interfaces that are often used in the actual environment:

type error interface {   Error() string}

Yes, it's error. Before that, we didn't even consider what to do if the code went wrong. So this is the time to fill this loophole. Golang no try. Catch.. Finally All errors are handled by error, and the classic code is as follows:

err := xxxxxxif err != nil {    ....}

This is basically the case, then the high force lattice, is to put the err in various types of encapsulation. But basically this is the way to deal with it. Golang also allows you to create error messages, such as:

func Sqrt(value float64)(float64, error) {   if(value < 0){      return 0, errors.New("Math: negative number passed to Sqrt")   }   return math.Sqrt(value)}

Users can pass errors. New () to create an error message with your own business logic. This is not much to start, because each team has its own coding standards, the handling of error is also strange, can not be unified description. But the processing mode is on top of that one, so end up with an actual code end error:

package mainimport "errors"import "fmt"import "math"func Sqrt(value float64)(float64, error) {   if(value < 0){      return 0, errors.New("Math: negative number passed to Sqrt")   }   return math.Sqrt(value), nil}func main() {   result, err:= Sqrt(-1)   if err != nil {      fmt.Println(err)   } else {      fmt.Println(result)   }      result, err = Sqrt(9)   if err != nil {      fmt.Println(err)   } else {      fmt.Println(result)   }}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.