Before starting to talk about the subject of this post let me make a small celebration for the 200th post on this blog. Lately I've slowed down the frequency of updates, but I hope to have increased the quality.
I think one feature that did turn up their nose to many is the lack
the keyword in the language class
Go
At first this illustrious absence might suggest the absence of the concept of object instead is very present and very helpful. But let
in order:
1) Definition of new types
Go allows you to define new types with the keyword type.
File type struct {
fileName [] bytes, / / \u200b\u200bName of the file
sizeInByte int, / / \u200b\u200bSize of the file
path [] bytes, / / \u200b\u200bLocation of the file on hd
In Thus we have associated with the file structure of a new type that from now on will have the attributes described herein.
2) Definizione di nuovi metodi
Creare nuovi metodi che eseguano del codice è molto semplice.
func PrintAll() {
fmt.Printf("All");
}
3) Associare i metodi ai tipi
A questo punto perchè non provare ad associare il metodo PrintAll al tipo File in modo che stampi tutti gli attributi di File quando viene invocato? Si deve quindi dire al metodo PrintAll che eseguerà operazioni solo sul tipo File. func (f * File) PrintAll()
{ fmt.Printf("Filename: %s\n",f.fileName); fmt.Printf("Size: % D \\ n ", f.sizeInByte)
fmt.Printf (" Path:% s \\ n ", f.path);}
4) instantiate a new File
This will can then instantiate a new file object and invoke its method. filePtr: = new (File) / / * File filePtr.fileName = "Useless.txt"
filePtr.sizeInByte = "1024";
filePtr.path = "$ HOME";
/ / Call method filePtr.PrintAll File ();
---------------------------------- --------------------
The argument is obviously much larger in this post but I wanted to give an introduction to using only the objects within Go to the most skeptical. Soon we will also talk about interfaces but leave before we get all the prejudices that come from other languages \u200b\u200bas Go addresses these issues from another point of view.
0 comments:
Post a Comment