Home  >  Article  >  Backend Development  >  Demonstrate the power of Go-Spring by building a minimal Web API

Demonstrate the power of Go-Spring by building a minimal Web API

藏色散人
藏色散人forward
2021-12-03 09:37:372867browse

This article is provided by the go language tutorial column to introduce how to use Go-Spring to build a minimal Web API. I hope it will be helpful to friends in need!

To Gopher

The following is the Hello World! program implemented using the Go standard library. There is really little code!
package mainimport (
  "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. The Hello World! program it implements is as follows. It's also very simple.

package mainimport (
  "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 take a look at the Hello World! program implemented using Go-Spring. Also very simple.

package mainimport (
  "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 there is an anonymously imported package in the example implemented using Go-Spring. Its function is to tell the Hello World! program to use Gin as the underlying Web Server implementation. If we change this line to the following code, the program can still execute normally, but 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 gained 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 automatic configuration capabilities that the standard library does not have, and compared with Java Spring Boot, Go-Spring's programming efficiency is not bad. The following is a Hello World! program implemented using Java Spring Boot, but unlike the above example, in order to demonstrate the dependency injection capabilities of Java Spring, it also prints the value of the JAVA_HOME environment variable. code show as below.

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;@RestControllerclass MyController {

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

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

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

The following is a program that uses Go-Spring's dependency injection capability to print the GOPATH environment variable and Hello World! at the same time. code show as below.

package mainimport (
  "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 truly realizes the integration of Go and Java Spring, keeping Go (syntax) simple while having the powerful configuration capabilities of Java Spring.

Through the introduction of this article, are you interested in Go-Spring? Hurry up and give it a try!

The above is the detailed content of Demonstrate the power of Go-Spring by building a minimal Web API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete