从域输入文件创建一个变量。

huangapple go评论69阅读模式
英文:

Create a variable from domains input file

问题

我正在尝试在Go语言中创建一个变量,该变量将从我的列表"Domains.txt"中获取域名输入的开头(在'.com'或'.net'之前)。

我尝试创建的变量是"%DomainName%",如果"Domains.txt"中的一个域名是'qwerty.com',那么%DomainName% := qwerty。

非常感谢您的帮助,

谢谢。


我尝试使用read()函数和scan()函数,但没有成功。

英文:

I am trying to create a variable in Go that will take the beginning of the domains input (before the '.com' or '.net') from my list "Domains.txt"

The variable I am trying to create is "%DomainName%", if one of the Domain inside Domains.txt is 'qwerty.com' then %DomainName% := qwerty

Your help would be very appreciated,

Thank you.


I tried the read() function and also the scan() function without success.

答案1

得分: 2

你可以使用strings.Split()函数来实现这个功能。下面是一个示例代码片段:

package main

import (
	"fmt"
	"strings"
)

func main() {
	domain := "qwerty.com"
	parts := strings.Split(domain, ".")
	domainName := parts[0]
	fmt.Println(domainName)
}

这段代码将在"."处将domain字符串拆分,并将第一部分赋值给domainName变量。请查看https://pkg.go.dev/strings#Split文档以获取有关strings.Split()函数的更多信息。

英文:

You can use the strings.Split() function to achieve this. Here's an example code snippet:

···

package main

import (
"fmt"
"strings"
)

func main() {
	domain := "qwerty.com"
	parts := strings.Split(domain, ".")
	domainName := parts[0]
	fmt.Println(domainName)
}

···

This code splits the domain string at the ".", and assigns the first part to the domainName variable. Check the https://pkg.go.dev/strings#Split documentation for more information on the strings.Split() function.

huangapple
  • 本文由 发表于 2023年3月25日 00:14:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75835983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定