0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go: 自作したモジュールの使い方

Posted at

次のページを参考にしました。
ローカル環境のみでの Go Modules と自作パッケージ

作業フォルダーの準備

mkdir module_test && cd module_test
go mod init gin_work
go get -u github.com/gin-gonic/gin

次のようにプログラムを作成

$ tree
.
├── go.mod
├── go.sum
├── main.go
├── module01
│   └── module01.go
├── module02
│   └── module02.go
└── module03
    └── module03.go
main.go
package main

import "github.com/gin-gonic/gin"
import "module_test/module01"
import "module_test/module02"
import "module_test/module03"

// ---------------------------------------------------------------
func main() {
    rr := gin.Default()
    rr.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })

    rr.GET("/morning", func(c *gin.Context) {
	str_out := module01.Prog01()
        c.JSON(200, gin.H{
            "message": str_out,
        })
    })


    rr.GET("/afternoon", func(c *gin.Context) {
	str_out := module02.Prog02()
        c.JSON(200, gin.H{
            "message": str_out,
        })
    })

    rr.GET("/evening", func(c *gin.Context) {
	str_out := module03.Prog03()
        c.JSON(200, gin.H{
            "message": str_out,
        })
    })

    rr.Run()
}

// ---------------------------------------------------------------
module01/module01.go
package module01

func Prog01() string {
	rvalue := "Morning"

	return rvalue
}
module02/module02.go
package module02

func Prog02() string {
	rvalue := "Afternoon"

	return rvalue
}
module03/module03.go
package module03

func Prog03() string {
	rvalue := "Evening"

	return rvalue
}

サーバーの実行

go run main.go

クライアントでアクセス

curl http://localhost:8080/morning
curl http://localhost:8080/afternoon
curl http://localhost:8080/evening
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?