JAPANESE
https://neovisionconsulting.blogspot.com/2022/05/vwasmvvgographqlaiwebwebtool20220504.html
ー--
WASM V language
GraphQL Go language
AI (Machine Learning) Go Language
Go language Easy to write Easy to learn even for beginners Easy to understand Grammar is simple and easy to understand
The Go language does not have Classes, but there are alternatives.
https://www.kwbtblog.com/entry/2020/04/07/055735
Key point: the
Combine multiple variables into one group (member variables)
Define functions that can operate on them (member functions)
-----
2020-04-07
Notes on things I got stuck on in Go language (classes and inheritance)
Go Golang Go Language Classes Pointers Inheritance
I started to learn Golang.
When I started, I found that Golang is designed to have as simple a syntax as possible and as simple a code as possible.
And because of that, Golang has some slightly tricky syntax.
However, without knowing them, I have fallen into a bit of trouble because I made judgments based on my preconceived notions and assumptions from other languages.
Here, I'd like to write down some of the things I personally got into or misunderstood when I started using Golang, as a reminder for each topic.
This is a note on classes and inheritance in Golang.
Golang has no classes
It is said that Golang does not have classes, and indeed it does.
However, there are things that come close to classes.
To begin with, the main things you want to do with classes are usually things like the following.
Combine multiple variables into one group (member variables)
Define functions that can operate on the grouped variables (member functions)
In Golang, a "structure" is used to group multiple variables into a single group, and a special function can be defined to manipulate the grouped items, which is tied to the structure.
For example, a class with member variables "x" and "y" and a member function "test()" to get their sum can be created as follows.
package main
import "fmt"
// Put the variables in a structure
type myStruct struct {
x int
y int
}
// function to manipulate the structure call it with the structure's ". operator
func (p *myStruct) test() int {
return p.x + p.y
}
func main() {
var a myStruct
a.x = 1
a.y = 2
fmt.Println(a.test()) // 3
}
The "test()" function is a little unfamiliar, but this function is a member function called from the structure's ". operator of the structure.
Since the elements of a structure can be accessed by anyone from the outside, the same thing can be achieved by writing a function that takes a structure as an argument and manipulates the structure, without defining member functions.
package main
import "fmt"
type myStruct struct {
x int
y int
}
// Manipulate a structure by taking a structure as an argument
func test(p *myStruct) int {
return p.x + p.y
}
func main() {
var a myStruct
a.x = 1
a.y = 2
fmt.Println(test(&a)) // 3
}
However, even with structs, you can write their operation functions with the ". operator so that you can write those operation functions with the "." operator, just like a class.
When written this way, it looks at first glance like a member function of the class.
In reality, the function is manipulated by passing a structure (or pointer to a structure) to the function, but it is the class in Golang that allows the function to be written as if it were a member function of the structure.
Inheritance
As mentioned above, there are no classes in Golang, but rather "struct + struct-operated function = class-like function".
There are times when you want to extend an already defined class-like structure by adding variables or operation functions. This is what is called inheritance in a class.
In Golang, you can also do such inheritance-like things.
If you register a parent structure as an element of a child structure with no element name but only the name of the parent structure, you can access the parent structure by using the element name of the parent structure name.
It is difficult to understand if written in text, so the code is as follows.
package main
import "fmt"
// parent struct
type parentStruct struct {
x int
y int
}
// function of parent struct
func (p *parentStruct) testParent() {
Println("Parent", p.x, p.y)
}
// childStruct
type childStruct struct {
z int
parentStruct // parent structure
}
// function of child struct
func (p *childStruct) testChild() {
// access a variable in the parent structure
fmt.Println("Child", p.parentStruct.x, p.parentStruct.y, p.z)
}
func main() {
var a childStruct
a.parentStruct.x = 1 // access parent variable
a.parentStruct.y = 2 // access parent variable
a.z = 3 // access child variable
a.parentStruct.testParent() // access parent 1 2 parent function
a.testChild() // access child 1 2 3 child functions
}
Inheritance
Looking at the aforementioned code, the child structure is not an inheritance of the parent structure, but rather just an element of the parent structure, so it seems natural that this would be the case.
But here is the key.
If the above structure is written in the way described above, member variables and functions of the parent structure can be called by writing member variables and functions directly under the child structure.
In other words, the above code can be written as follows, omitting the parent structure name.
package main
import "fmt"
// parent struct
type parentStruct struct {
x int
y int
}
// function of parent struct
func (p *parentStruct) testParent() {
Println("Parent", p.x, p.y)
}
// childStruct
type childStruct struct {
z int
parentStruct
}
// function of the child structure
func (p *childStruct) testChild() {
// access variables in parent structure
Println("Child", p.x, p.y, p.z) // can omit parent structure modifiers
}
func main() {
var a childStruct
a.x = 1 // access parent's variables, can omit parent struct modifiers
a.y = 2 // access parent variable can omit parent struct modifier
a.z = 3
a.testParent() // Parent 1 2 access parent function can omit parent struct modifier
a.testChild() // Child 1 2 3
}
When written this way, it looks at first glance like inheritance.
In reality, it is not inheritance because the parent structure is just an element, but it is inheritance in Golang that makes it look as if it is inherited.
What if the name of the element and the name of the function are covered?
What happens if a child structure defines an element or function with the same name as the parent structure?
In that case, the element/function of the child structure is used. Access to the elements and functions of the parent structure is done by specifying the name of the parent structure, as described in the first step.
package main
import "fmt" import
type parentStruct struct {
x int
y int
}
func (p *parentStruct) test() {
Println("Parent", p.x, p.y)
}
type childStruct struct {
x int
parentStruct
}
func (p *childStruct) test() {
// x is a duplicate, so to access x in the parent structure, specify the parent structure
fmt.Println("Child", p.parentStruct.x, p.y, p.x)
}
func main() {
var a childStruct
// x is a duplicate, so to access x in the parent structure, specify the parent structure
a.parentStruct.x = 1
a.y = 2
a.x = 3
// test() is a duplicate, so to access test() of the parent structure, specify the parent structure
a.parentStruct.test() // Parent 1 2
a.test() // Child 1 2 3
}
In other words, "the member variables and functions of the parent structure can be called by writing member variables and functions directly under the child structure" because the compiler looks at the code and completes the parent structure names behind the scenes for goodness' sake.
Usage of Inheritance
There are times when you want to add elements or functions to a structure that has already been created as an object. In such cases, inheritance is a good way to do it.
For example, let's say you want to extend the http.Clinet structure to hold the results of a Google search.
package main
import (
"fmt".
"io/ioutil"
"net/http"
)
// inherit a structure with new functionality
type googleSearch struct {
result string // result
*Client // pointer to the inherited structure
}
// add search() function search results are stored in result
func (p *googleSearch) search(q string) {
resp, err := p.Get(fmt.Sprintf("https://www.google.com?q=%s", q))
if err ! = nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err ! = nil {
return
}
p.result = string(body)
}
func main() {
c := &http.Client{}
g := googleSearch{Client: c} // inherit from http.
g.search("test")
fmt.Println(g.result)
}
There are many cases where structures are retrieved by pointers, and in those cases, the key is to make sure that the parent structure is received by pointers.
When creating an extended structure, assign the pointer of the parent structure to the pointer that has already been retrieved.
Then you can access the elements/functions of the parent structure directly from the child structure.
Comments, etc.
Since structures and their operation functions are defined independently in different places, it is more difficult to find out what operation functions are available for a structure than for a class.
In fact, structures are structures and functions are functions, and the compiler only ties them together in the coding, and the structure is not involved in what its member functions are.
Inheritance is not inheritance, it is simply a structure with elements of the parent structure. I misunderstood this and got stuck. It is easy to get hooked if you know how to omit the parent struct name first.
Golang's syntax is primitive, but it allows you to write and omit in various ways, making it equivalent to a high-performance language.
Hmmm...Golang is a reasonable and quite interesting language.
0 コメント:
コメントを投稿