I used to write JAVA. I used JAVA during the four years of college from my sophomore to my senior year. Now for some reasons, I need to learn GO, so I started I created a new column to record some of my experiences in converting JAVA to GO. My writing is not good and I have little experience in writing blogs. My expression may not be particularly clear. If there are any errors in the article, I hope you guys can point them out.
Article Table of Contents
- Structure
-
- Declaration of structure
- Defining methods of structures
- Interfaces
-
- Declaration of interfaces
- Implementation of inheritance in Go (embedding)
- Serialization
Structure
Although GO is also a The language of objects, but unlike JAVA and C, there is no Class keyword in GO, but struct is used instead. Some differences between struct and class are listed below.
Declaration of structure
h2>
Use type Name struct{} to declare a structure in GO, as follows
type Dog struct{
name string
}
The above code defines a Dog structure, which contains a name field. The type of name is string.
The definition of variables in the go language is quite special. Using variable name type
to declare a variable may be very uncomfortable when you first come into contact with it.
How to define a structure
In GO, the way to define member methods is different from that in JAVA. It is defined outside the structure, as follows
func (d Dog) sleep() {
fmt.Printf("Dog %s is sleeping", d. span>name)
}
The above The code defines a sleep() method for the Dog structure. The parentheses before the method name can be understood as the receiver of this method. Then we can call the member methods of this structure through d.sleep()
func main(){
d : = Dog{name: "snoopy"}
d.sleep()
}
Interface
Interface declaration
In GO, the way to declare an interface is
type Sleeper interface{
sleep()
}
The above code defines an interface named Sleeper, and other structures that want to implement this interface only need Just implement the methods in this interface, as follows
func (d Dog) sleep( ) {
fmt.Printf("Dog %s is sleeping", d.name)
}
You can test whether Sleeper can be converted to Dog in the main method
func main(){
var s Sleeper
s = Dog{name: "snoopy"}
s .sleep()
}
As you can see, a Sleeper is first declared in the main method Interface type s, then instantiate s as a Dog object, and call the sleep() method of s. The execution result is as follows:
Dog snoopy is sleeping
Implementation of inheritance in Go (embedded)
Unlike JAVA, Go does not use the extends keyword to inherit a class, but Inheritance is achieved through embedding, as shown in the following code. By embedding the Animal structure into the Dog structure, the Dog structure inherits the fields and methods of the Animal structure
type Animal struct{
age int
gender int
}
type Dog struct{
Animal
name string
}
func (a Animal) bark(){
fmt.Println("animal is barking!")
}
func (d Dog) bark(){
fmt .Println("Wooo")
}
After that, you can add the Dog type Variables and methods for calling Animal in variables
func main (){
dog := Dog{name: "snoopy"}
fmt.Printf("The age of snoopy is %d \n", dog.age) // The default value of the unassigned int type in Go is 0, and string is the empty string
dog.age = 7
fmt.Printf ("The age of snoopy is %d \n", dog.age) // Assigned
dog.Animal.bark()
dog.bark()
}
Result:
The age of snoopy is 0
The age of snoopy is 7
animal is barking!
Wooo
Serialization
In JAVA, if we want to persist a class, we must make the class implement Serializable
Interface, that is, serialization. In Go, the serialization method is as follows:
type Dog struct{
Name string `json:"name"`
Age int `json:"age" `
}
Note that the name of the variable here must start with a capital, otherwise it will be serialized later It will be lost. I don’t know why at the moment. I hope someone who knows the reason can point it out in the comment area
func main(){
d := Dog{Name: "snoopy", Age: 7}
dJson, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(dJson))
}
Execution result:
{"name":"snoopy", "age":7}
p>
type Dog struct{
Name string `json:"name"`
Age int `json:"age"`
}
Note that the name of the variable here must start with a capital, otherwise it will be lost after serialization. I don’t know why at the moment. I hope someone who knows the reason can comment in the comment area. Point out
func main(){
d := Dog{Name: "snoopy" , Age: 7}
dJson, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
fmt.Println(string span>(dJson))
}
Execution result:
{"name": "snoopy","age":7}