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)) 
} 

运行结果:

相关推荐
梵刹古音1 分钟前
【C语言】 格式控制符与输入输出函数
c语言·开发语言·嵌入式
Acrelhuang8 分钟前
工商业用电成本高?安科瑞液冷储能一体机一站式解供能难题-安科瑞黄安南
大数据·开发语言·人工智能·物联网·安全
hello 早上好8 分钟前
03_JVM(Java Virtual Machine)的生命周期
java·开发语言·jvm
沐雪架构师9 分钟前
LangChain 1.0 Agent开发实战指南
开发语言·javascript·langchain
tod11310 分钟前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode
2501_9400078921 分钟前
Flutter for OpenHarmony三国杀攻略App实战 - 战绩记录功能实现
开发语言·javascript·flutter
naruto_lnq21 分钟前
C++中的桥接模式
开发语言·c++·算法
无限进步_23 分钟前
面试题 02.02. 返回倒数第 k 个节点 - 题解与详细分析
c语言·开发语言·数据结构·git·链表·github·visual studio
布谷歌31 分钟前
面试题整理
java·开发语言
j4455661140 分钟前
C++中的职责链模式高级应用
开发语言·c++·算法