首页 > 其他分享 >[Go] Get used to return (*SomeType, error) as function return type

[Go] Get used to return (*SomeType, error) as function return type

时间:2024-02-19 16:15:04浏览次数:29  
标签:function p2 used return err nil p1 line

package main

import (
	"fmt"
	"log"
	"strconv"
	"strings"
)

type Point struct {
	x int
	y int
}

type Line struct {
	p1 *Point
	p2 *Point
}

func getInput() string {
  return `0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2`;
}

func parsePoint(line string) (*Point, error) {
	ps := strings.Split(line, ",");
	x, err := strconv.Atoi(ps[0]);
	if err != nil {
		return nil, err
	}

	y, err := strconv.Atoi(ps[1]);
	if err != nil {
		return nil, err
	}

	return &Point{x, y}, nil
}

func parseLine(line string) (*Line, error) {
	l := strings.Split(line, " -> ")
	p1, err := parsePoint(l[0])
	if err != nil {
		return nil, err
	}

	p2, err := parsePoint(l[1])
		if err != nil {
		return nil, err
	}

	return &Line {
		p1: p1, 
		p2: p2,
	}, nil
}

func isHOrV (line Line) bool {
	return line.p1.x == line.p2.x || line.p1.y == line.p2.y
}

func main() {
	lines := []Line {}
	for _, l := range strings.Split(getInput(), "\n") {
		line, err := parseLine(l)
		if err != nil {
			log.Fatal("Cannot parse the line")
		}
		if isHOrV(*line) {
			lines = append(lines, *line)
		}
	}

  // Print each line with actual values of points
  for _, line := range lines {
    fmt.Printf("Line from (%d,%d) to (%d,%d)\n", line.p1.x, line.p1.y, line.p2.x, line.p2.y)
  }
}

 

标签:function,p2,used,return,err,nil,p1,line
From: https://www.cnblogs.com/Answer1215/p/18021335

相关文章

  • 适用于 Amazon Step Functions 的低代码可视化新工作流 Workflow Studio, 现已在 Amaz
    今天,我们非常欣喜地宣布现已在AmazonApplicationCompose中推出AmazonStepFunctionsWorkflowStud。通过这款全新的集成应用,工作流与应用程序资源开发便可整合到统一的可视化基础设施即代码(IaC)生成器。对于使用AmazonStepFunctionsWorkflowStudio创建工作流与......
  • CF1879D Sum of XOR Functions
    记前缀异或和数组\(s\),于是题目中的\(f(l,r)\)可以被表示成\(s_r\opluss_{l-1}\),转化为求\(\sum\limits_{l=1}^n\sum\limits_{r=l}^n(s_r\opluss_{l-1})\times(r-l+1)\)。再记录\(4\)个值,\(cnt_0,cnt_1,sum_0,sum_1\)分别表示当前已经出现过的\(0/1\)的个数,出......
  • 阿克曼函数(Ackermann function)部分推导
    相关题目已知\(Ackermannfunction\)为Ack(m,n)={n+1__m=0;Ack(m-1,1)__m>0&&n=0;Ack(m-1,Ack(m,n-1)__m>0&&n>0.}当\(m=1\)时有\(Ack(1,n)\)\(=Ack(0,Ack(1,n-1))=Ack(1,n-1)+1;\)\(=Ack(0,Ack(1,n-2))+1=Ack(1,n-2)+2;\)\(=···\)......
  • [Rust] Macros vs. Functions
    InRust,theexclamationmark(!)afteranameindicatesthatitisamacroratherthanafunction.MacrosandfunctionsinRustarecalleddifferentlyandservedifferentpurposes.Macros,likeanyhow!,candothingsthatfunctionscannot,suchasgenera......
  • Go 100 mistakes - #11: Not using the functional options pattern
      Here,WithPortreturnsaclosure.Aclosureisananonymousfunctionthatreferences variablesfromoutsideitsbody;inthiscase,theportvariable.Theclosurerespectsthe Optiontypeandimplementstheport-validationlogic.Eachconfigfieldr......
  • Go 100 mistakes - #9: Being confused about when to use generics
    Go1.18addsgenericstothelanguage.Inanutshell,thisallowswritingcodewithtypes thatcanbespecifiedlaterandinstantiatedwhenneeded. Onelastthingtonoteabouttypeparametersisthattheycan’tbeusedwith methodarguments,onlywith......
  • Go 100 mistakes - #7: Returning interfaces
       Allinall,inmostcases,weshouldn’treturninterfacesbutconcreteimplementa-tions.Otherwise,itcanmakeourdesignmorecomplexduetopackagedependencies andcanrestrictflexibilitybecausealltheclientswouldhavetorelyonthesam......
  • Wicket:Refused to apply inline style because it violates the following Content S
    Content Security Policy (CSP)是一种加固Web应用的安全性的技术,通过在网站页面中设置CSPHeader来限制页面中能够执行的脚本、样式、图片等资源。CSP包...参考1:https://blog.csdn.net/qq_30436011/article/details/127485927为什么我们需要CSP?参考2:https://blog.csd......
  • 【Azure Function】Azure Function中使用 Java 8 的安全性问题
    问题描述使用AzureFunction,环境是Linux的Java8。目前OracleJavaJDK8,11,17和OpenJDK8/11/17都在存在漏洞受影响版本的范围内。OpenJDK                 CVEnumbers:    CVE‑2023‑21954CVE‑2023‑21938CVE‑2023‑21937CVE......
  • less报错,Error evaluating function `unit`: the first argument to unit must be a n
    1、less-loader版本太高了,可以降低版本2、lessc的执行参数里面增加一个参数--math=always3、可以关闭严格模式{loader:"less-loader",options:{lessOptions:{strictMath:false,},},}, 4、vite配置可以设置math:"always",......