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 1 year has passed since last update.

Goプロジェクトの作成〜サーバーへのアクセスまで

Last updated at Posted at 2023-05-29

APIやらバッチやらなんだかんだGoは1年半以上書いていますが、
Goのサーバは0ベースで書いたことないなと思い、簡単に立ててみようかなと。

作成手順

リポジトリの取得

リポジトリのClone。

$ git clone git@github.com:mitsuaki1229/GoServerExample.git
$ cd GoServerExample

サーバの作成

mainの作成。

$ touch main.go
$ vim main.go

echoでサーバの中身の記載。

package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

var e = createMux()

func main() {
	e.GET("/", articleIndex)

	e.Logger.Fatal(e.Start(":8080"))
}

func createMux() *echo.Echo {
	e := echo.New()

	e.Use(middleware.Recover())
	e.Use(middleware.Logger())
	e.Use(middleware.Gzip())

	return e
}

func articleIndex(c echo.Context) error {
	return c.String(http.StatusOK, "Hello, World!")
}

プロジェクトの初期化

パスを指定しないとエラーになる。

$ go mod init
go: cannot determine module path for source directory /Users/mitsuaki1229/Documents/90_Other/repo/GoServerExample (outside GOPATH, module path must be specified)

GitHubのパスを指定して再実行。

$ go mod init github.com/mitsuaki1229/GoServerExample
go: creating new go.mod: module github.com/mitsuaki1229/GoServerExample
go: to add module requirements and sums:
	go mod tidy

モジュールのインポート。

$ go mod tidy
go: finding module for package github.com/labstack/echo/v4/middleware
go: finding module for package github.com/labstack/echo/v4
go: downloading github.com/labstack/echo/v4 v4.10.2
go: downloading github.com/labstack/echo v3.3.10+incompatible
go: found github.com/labstack/echo/v4 in github.com/labstack/echo/v4 v4.10.2
go: found github.com/labstack/echo/v4/middleware in github.com/labstack/echo/v4 v4.10.2
go: downloading github.com/labstack/gommon v0.4.0
go: downloading golang.org/x/crypto v0.6.0
go: downloading golang.org/x/net v0.7.0
go: downloading github.com/stretchr/testify v1.8.1
go: downloading github.com/valyala/fasttemplate v1.2.2
go: downloading golang.org/x/time v0.3.0
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/mattn/go-isatty v0.0.17
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/valyala/bytebufferpool v1.0.0
go: downloading golang.org/x/text v0.7.0
go: downloading golang.org/x/sys v0.5.0

起動

$ go run main.go

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v4.10.2
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:8080

アクセス確認

Screen Shot 2023-05-29 at 20.53.00.png

感想

Spring BootRuby on Railsと比べると、シンプルに出来るのが良いですね。

参考

参照

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?