go:运行第一个go语言程序

1.如何创建go语言编辑界面

2.案例一实现简单打印"hello worlg":

Go 复制代码
package main 
 
import "fmt" 
 
func main() { 
    for i := 0; i < 10; { 
        if i < 0 { 
            continue 
        } 
        fmt.Println("hello world") 
        i++ 
    } 
} 

运行结果:

Go 复制代码
PS D:\demo2> go mod init DEMO2
go: creating new go.mod: module DEMO2
PS D:\demo2> go build
no Go files in D:\demo2
PS D:\demo2> go build
PS D:\demo2> go run main.go
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

3.案例二实现字符转化为二维码并渲染到web页面

Go 复制代码
 
package main 
 
import ( 
	"fmt" 
	"image/png" 
	"log" 
	"net/http" 
 
	"github.com/skip2/go-qrcode"  
) 
 
// 处理二维码生成和页面渲染 
func generateQRCodeHandler(w http.ResponseWriter, r *http.Request) { 
	if r.Method == http.MethodPost { 
		// 获取表单中输入的文本 
		text := r.FormValue("text") 
		if text == "" { 
			http.Error(w, "请输入要转换为二维码的文本", http.StatusBadRequest) 
			return 
		} 
 
		// 生成二维码图片 
		qr, err := qrcode.New(text, qrcode.Medium) 
		if err!= nil { 
			http.Error(w, "生成二维码时出错", http.StatusInternalServerError) 
			return 
		} 
 
		// 设置响应头,指定返回的是 PNG 图片 
		w.Header().Set("Content-Type", "image/png") 
		// 将二维码图片编码为 PNG 格式并写入响应 
		if err := png.Encode(w, qr.Image(256)); err!= nil { 
			http.Error(w, "写入二维码图片时出错", http.StatusInternalServerError) 
			return 
		} 
		return 
	} 
 
	// 显示 HTML 表单页面 
	fmt.Fprint(w, ` 
	<!DOCTYPE html> 
	<html lang="en"> 
	<head> 
		<meta charset="UTF-8"> 
		<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
		<title>生成二维码</title> 
	</head> 
	<body> 
		<h1>请输入要转换为二维码的文本</h1> 
		<form method="post"> 
			<input type="text" name="text" placeholder="输入文本"> 
			<button type="submit">生成二维码</button> 
		</form> 
	</body> 
	</html> 
	`) 
} 
 
func main() { 
	// 注册处理函数 
	http.HandleFunc("/", generateQRCodeHandler) 
 
	// 启动服务器,监听 8080 端口 
	log.Println("Server started at http://localhost:8080") 
	log.Fatal(http.ListenAndServe(":8080", nil)) 
} 

运行结果:

相关推荐
Q_Q19632884751 分钟前
python基于Hadoop的超市数据分析系统
开发语言·hadoop·spring boot·python·django·flask·node.js
暮乘白帝过重山3 分钟前
负载因子(Load Factor) :哈希表(Hash Table)中的一个关键性能指标
开发语言·数据结构·哈希算法·散列表·负载因子·暮乘白帝过重山
山中月侣4 分钟前
java集合 之 多列集合
java·开发语言·经验分享·学习方法
小乌龟不会飞12 分钟前
【SpringBoot】统一功能处理
java·spring boot·后端
刘小吉16 分钟前
java net 配置局域网受信任的https
后端
MediaTea23 分钟前
Python 第三方库:Requests(HTTP 客户端)
开发语言·网络·python·网络协议·http
考虑考虑27 分钟前
JPA中的EntityGraph
spring boot·后端·spring
coolflyr_reg32 分钟前
禅道集成Firebase PHP-JWT
后端
似水流年流不尽思念36 分钟前
常见的排序算法有哪些?它们的平均时间复杂度是多少?
后端·算法
孟永峰_Java1 小时前
MySQL 组合IN查询:你的索引为什么罢工了?
后端