通过构建最小Web API来展示Go-Spring的强大

藏色散人
Freigeben: 2021-12-03 09:37:37
nach vorne
2824 Leute haben es durchsucht

本文由go语言教程栏目给大家介绍如何使用 Go-Spring 构建最小 Web API ,希望对需要的朋友有所帮助!

前言

Go 语言以简单著称,一个很明显的例子就是只需要很少的代码即可实现一个最小的 Web API 。Go-Spring 融合了 Go 简单和 Spring 自动配置的优点。本文通过几个实现最小 Web API 的示例展示 Go-Spring 的简单和强大。

To Gopher

下面是使用 Go 标准库实现的 Hello World! 程序。代码真的很少!

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

Gin 是目前最火的 Web 框架之一,它实现的 Hello World! 程序如下。也很简单。

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()}
Nach dem Login kopieren

再来看看使用 Go-Spring 实现的 Hello World! 程序。同样很简单。

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()}
Nach dem Login kopieren

但是,可以注意到使用 Go-Spring 实现的示例中有一个匿名导入的包,它的作用是告诉 Hello World! 程序使用 Gin 作为底层 Web Server 实现。如果我们把这一行改为如下代码,程序仍然可以正常执行,但是这时候程序使用 Echo 作为底层 Web Server 实现。

_ "github.com/go-spring/starter-echo"
Nach dem Login kopieren

虽然 Go-Spring 多了一行匿名包导入,但因此获得了比标准库更强大的能力。

To Javaer

Go-Spring 虽然提供了和 Go 标准库一样的编程模型,但本质上它是基于 IoC (依赖注入) 实现的,因此它具有标准库不具备的自动配置能力,而且与 Java Spring Boot 相比,Go-Spring 的编程效率也不差。

下面是使用 Java Spring Boot 实现的一个 Hello World! 程序,但是与上面的示例不同,为了展示 Java Spring 的依赖注入能力,它同时会打印 JAVA_HOME 环境变量的值。代码如下。

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); }}
Nach dem Login kopieren

下面是使用 Go-Spring 的依赖注入能力实现的能同时打印 GOPATH 环境变量和 Hello World! 的程序。代码如下。

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()}
Nach dem Login kopieren

比较上面两个示例,可以看出 Go-Spring 真正实现了 Go 和 Java Spring 的融合,在保持 Go (语法) 简单的同时具备 Java Spring 的强大配置能力。

通过本文的介绍,你有没有对 Go-Spring 动心呢?赶紧动手试试吧!

Das obige ist der detaillierte Inhalt von通过构建最小Web API来展示Go-Spring的强大. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
go
Quelle:learnku.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!