Building minimal web APIs using go spring

preface

The go language is famous for its simplicity. An obvious example is that it requires very little code to implement a minimal Web API. Go Spring combines the advantages of go simplicity and Spring automatic configuration. This article demonstrates the simplicity and power of go Spring through several examples that implement the smallest Web API.

To Gopher

The following is the Hello World implemented using the Go standard library! Procedure. Code is really few!

package main

import (
  "net/http"
)

func main() {
  http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello World!"))
  })
  http.ListenAndServe(":8080", nil)
}

Gin is one of the most popular Web frameworks at present. It implements Hello World! The procedure is as follows. It's also very simple.

package main

import (
  "github.com/gin-gonic/gin"
  "github.com/gin-gonic/gin/ginS"
)

func main() {
  ginS.GET("/", func(c *gin.Context) {
    c.String(200, "Hello World!")
  })
  ginS.Run()
}

Let's look at the Hello World implemented with go spring! Procedure. It's also simple.

package main

import (
  "github.com/go-spring/spring-core/gs"
  "github.com/go-spring/spring-core/web"
  _ "github.com/go-spring/starter-gin"
)

func main() {
  gs.GetMapping("/", func(ctx web.Context) {
    ctx.String("Hello World!")
  })
  gs.Run()
}

However, you can notice that in the example implemented using go spring, there is an anonymous imported package, which is used to tell Hello World! The program uses Gin as the underlying Web Server implementation. If we change this line to the following code, the program can still execute normally, but at this time, the program uses Echo as the underlying Web Server implementation.

_ "github.com/go-spring/starter-echo"

Although go spring has one more line of anonymous package import, it has more powerful capabilities than the standard library.

To Javaer

Although Go spring provides the same programming model as the Go standard library, it is essentially implemented based on IoC (dependency injection), so it has the ability of automatic configuration that the standard library does not have, and the programming efficiency of Go spring is not poor compared with Java Spring Boot.

The following is a Hello World implemented using Java Spring Boot! Program, but different from the above example, in order to show the dependency injection capability of Java Spring, it will print Java at the same time_ The value of the home environment variable. The code is as follows.

package com.example.demo11;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
class MyController {

  @Value("${JAVA_HOME}")
  String JavaHome;

  @GetMapping("/")
  public String hello() {
    return this.JavaHome + " - Hello World!";
  }

}

@SpringBootApplication
public class Demo11Application {

  public static void main(String[] args) {
    SpringApplication.run(Demo11Application.class, args);
  }

}

The following is implemented using the dependency injection capability of go spring, which can print GOPATH environment variables and Hello World at the same time! Procedures. The code is as follows.

package main

import (
  "github.com/go-spring/spring-core/gs"
  "github.com/go-spring/spring-core/web"
  _ "github.com/go-spring/starter-gin"
)

func init() {
  gs.Object(new(MyController)).Init(func(c *MyController) {
    gs.GetMapping("/", c.Hello)
  })
}

type MyController struct {
  GoPath string `value:"${GOPATH}"`
}

func (c *MyController) Hello(ctx web.Context) {
  ctx.String(c.GoPath + " - Hello World!")
}

func main() {
  gs.Run()
}

Comparing the above two examples, we can see that Go spring really realizes the integration of Go and Java Spring, and has the powerful configuration ability of Java Spring while maintaining the simplicity of Go (Syntax).

Through the introduction of this article, are you interested in go spring? Try it quickly!

Official website and communication

Go spring official website
Github

Tags: Go ioc

Posted on Wed, 01 Dec 2021 15:12:22 -0500 by gdboling