Now that I am conscious of the package system, I will introduce Hello World

Next Steps to Hello World! Hello Servant!

The first step in advancing programming learning 「Hello World」 You do this the same way it used to be, right?

But modern programming is Package ... Package Anyway package Who controls the package system It is no exaggeration to say that it controls modern programming.

So this time, the modern programming version "Hello World" I would like to propose a learning method called "Hello Servant".

Trendy Go language

First of all, Hello World normally

main.go


package main

import (
	"fmt"
)

func main()  {
	fmt.Println("Hello World")
}
go run main.go
-> Hello World

Then "Hello World" for package system Let's try "Hello Servant". Create a directory called "servant" in the project root Well, it means "servant" to say hello to "Hello World" In the sense of a greeting person Let's make a package called "Greeter".

mkdir -p servant/Greeter
touch servant/Greeter/Greeter.go

servant/Greeter/Greeter.go


package Greeter

import (
	"fmt"
)

type Greeter struct {}


func Summon() *Greeter {
	return &Greeter{}
}

func (g *Greeter) ServeHello() {
	fmt.Println("Hello Servant!")
}

Rewrite main.go as follows.

main.go


package main

import (
	"<Module name given at the time of go mod init>/servant/Greeter"
)

func main()  {
	//Summon Greeter! !!
	greeter := Greeter.Summon()
	//service execution! !!
	greeter.ServeHello()
}
go run main.go
-> Hello Servant!

In Go language, the package system version "Hello World" was created like this.

Trendy Python

The directory structure looks like this

.
├── main.py
└── servant
    ├── Greeter.py
    └── __init__.py

servant/Greeter.py



class Summon:
    def __init__(self):
        self.message = "Hello Servant!"

    def serve_hello(self):
        print(self.message)

main.py


from servant import Greeter

#Summon Greeter! !!
greeter = Greeter.Summon()
#service execution! !!
greeter.serve_hello()
python main.py
-> Hello Servant!

Try it with TypeScript

Well, because it's TypeScript, it looks like this including the dist directory.

.
├── dist
│   ├── main.js
│   └── main.js.map
└── src
    ├── main.ts
    └── servant
        └── Greeter.ts

src/servant/Greeter.ts


export module Greeter {
    export class Summon {
        constructor() {}
        serveHello(): void {
            console.log('hello Servant!')
        }
    }
}

src/main.ts


import { Greeter } from './servant/Greeter'

const greeter = new Greeter.Summon()

greeter.serveHello()
parcel build src/main.ts
node dist/main.js

-> Hello Servant!

Summary

Well, learning programming After doing "Hello World" While trying to develop an application There are many people who are having a hard time learning because there is a gap. Isn't it?

This "Hello Servant" After this, for example

main.go


greeter := Greeter.Summon("Yamada Taro")
greeter.ServeHello()
->Hello Taro Yamada!

I tried to do that Create a servant called Scraper For example, from a site that lists athletes Pull the list of athlete names Try making a Greeter to greet in the language of the country according to the nationality of the athlete

Isn't the range of practice much wider than the ordinary Hello World?

By the way, this design concept of "servant" It is a design concept that is very compatible with people who have played Yu-Gi-Oh! Cards or card games. It's just saying "Summon". And it is also possible to fetch data from the web like scraping It's sensuously similar to the Yu-Gi-Oh card or something like "○○ on the field". Programming itself becomes fun as if you were already playing a card game.

Recommended Posts

Now that I am conscious of the package system, I will introduce Hello World
Study from the beginning of Python Hour1: Hello World
I evaluated the strategy of stock system trading with Python.
I want to know the legend of the IT technology world