Go isPrivateIP()

Jmnote (토론 | 기여)님의 2023년 4월 7일 (금) 19:42 판 (새 문서: ==개요== ;Go isPrivateIP() <syntaxhighlight lang='go' run> package main import ( "fmt" "net" ) func isPrivateIP(ip string) bool { parsed := net.ParseIP(ip) return parsed.IsPr...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

Go isPrivateIP()
package main

import (
	"fmt"
	"net"
)

func isPrivateIP(ip string) bool {
	parsed := net.ParseIP(ip)
	return parsed.IsPrivate() || parsed.IsLoopback()
}

func main() {
	// true
	fmt.Println(isPrivateIP("10.10.10.10"))  // private
	fmt.Println(isPrivateIP("172.27.30.30")) // private
	fmt.Println(isPrivateIP("192.168.0.1"))  // private
	fmt.Println(isPrivateIP("127.0.0.1"))    // loopback

	// false
	fmt.Println(isPrivateIP("8.8.8.8"))
	fmt.Println(isPrivateIP("172.0.0.1"))
	fmt.Println(isPrivateIP("135.79.246.88"))
	fmt.Println(isPrivateIP("192.192.192.192"))
}


2 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}